From 6f44cee3008fcc57bc82428422c8efdd254698c1 Mon Sep 17 00:00:00 2001 From: Paolo Predonzani Date: Tue, 14 Oct 2014 17:01:34 +0200 Subject: [PATCH] close #41 cmis entry abstraction --- .../amp/web/js/softwareloop/cmis/Entry.js | 73 +++++++++++++++ .../js/softwareloop/{util => cmis}/cmis.js | 1 - .../amp/web/js/softwareloop/inboxes/Inbox.js | 10 ++- .../amp/web/js/softwareloop/inboxes/Item.js | 90 ++++--------------- 4 files changed, 94 insertions(+), 80 deletions(-) create mode 100644 src/main/amp/web/js/softwareloop/cmis/Entry.js rename src/main/amp/web/js/softwareloop/{util => cmis}/cmis.js (98%) diff --git a/src/main/amp/web/js/softwareloop/cmis/Entry.js b/src/main/amp/web/js/softwareloop/cmis/Entry.js new file mode 100644 index 0000000..bb7ef9b --- /dev/null +++ b/src/main/amp/web/js/softwareloop/cmis/Entry.js @@ -0,0 +1,73 @@ +define([ + "dojo/_base/declare", + "dojo/_base/array", + "softwareloop/cmis/cmis", + "softwareloop/util/browser" +], function (declare, array, cmis, browser) { + return declare(null, { + constructor: function (entryNode) { + this.id = entryNode.getElementsByTagName("id")[0].firstChild.nodeValue.substring(9); + this.attributes = {}; + this.parseProperties(entryNode, "propertyId", + function (stringValue) { + return stringValue; + } + ); + this.parseProperties(entryNode, "propertyString", + function (stringValue) { + return stringValue; + } + ); + this.parseProperties(entryNode, "propertyInteger", parseInt); + + this.parseProperties(entryNode, "propertyBoolean", + function (stringValue) { + return stringValue === 'true'; + } + ); + this.parseProperties(entryNode, "propertyDateTime", cmis.parseDate); + }, + + parseProperties: function (entryNode, tagName, converter) { + var propertyStrings = + browser.getElementsByTagName(entryNode, "cmis", tagName); + for (var i = 0; i < propertyStrings.length; i++) { + var propertyString = propertyStrings[i]; + var cmisAttributeName = + propertyString.getAttribute("propertyDefinitionId"); + var attribute = { + tagName: tagName, + values: [], + value: function () { + return (this.values.length > 0) ? this.values[0] : null + } + }; + this.attributes[cmisAttributeName] = attribute; + var valueNode = browser.getElementsByTagName(propertyString, "cmis", "value"); + if (valueNode) { + array.forEach(valueNode, function (current) { + var cmisAttributeValue = null; + try { + var nodeValue = current.firstChild.nodeValue; + cmisAttributeValue = converter(nodeValue); + } catch (e) { + cmisAttributeValue = null; + } + attribute.values.push(cmisAttributeValue); + }); + } + } + }, + + getAttributeValues: function (name) { + var attribute = this.attributes[name]; + return attribute ? attribute.values : []; + }, + + getAttributeValue: function (name) { + var attributeValues = this.getAttributeValues(name); + return (attributeValues.length > 0) ? attributeValues[0] : null; + } + + }); +}); \ No newline at end of file diff --git a/src/main/amp/web/js/softwareloop/util/cmis.js b/src/main/amp/web/js/softwareloop/cmis/cmis.js similarity index 98% rename from src/main/amp/web/js/softwareloop/util/cmis.js rename to src/main/amp/web/js/softwareloop/cmis/cmis.js index 0e7a781..4b13dc8 100644 --- a/src/main/amp/web/js/softwareloop/util/cmis.js +++ b/src/main/amp/web/js/softwareloop/cmis/cmis.js @@ -48,7 +48,6 @@ define([ for (var cmisAttributeName in entryAttributes) { if (entryAttributes.hasOwnProperty(cmisAttributeName)) { var entryAttribute = entryAttributes[cmisAttributeName]; - console.log(entryAttribute); var elementName = "cmis:" + entryAttribute.tagName; xb.openElement(elementName); xb.addAttribute("propertyDefinitionId", cmisAttributeName); diff --git a/src/main/amp/web/js/softwareloop/inboxes/Inbox.js b/src/main/amp/web/js/softwareloop/inboxes/Inbox.js index 0ad595f..f31796f 100644 --- a/src/main/amp/web/js/softwareloop/inboxes/Inbox.js +++ b/src/main/amp/web/js/softwareloop/inboxes/Inbox.js @@ -16,9 +16,10 @@ define([ "dojo/_base/lang", "dojo/topic", "softwareloop/util/browser", - "softwareloop/util/cmis", + "softwareloop/cmis/cmis", + "softwareloop/cmis/Entry", "dojo/_base/array" -], function (TemplatedMixin, AttachMixin, WidgetBase, Core, declare, template, xhr, domClass, registry, hash, lang, topic, browser, cmis, array) { +], function (TemplatedMixin, AttachMixin, WidgetBase, Core, declare, template, xhr, domClass, registry, hash, lang, topic, browser, cmis, Entry, array) { return declare([WidgetBase, TemplatedMixin, Core, AttachMixin], { templateString: template, @@ -123,8 +124,9 @@ define([ try { var i; var items = []; - var entries = this.data.getElementsByTagName("entry"); - array.forEach(entries, function (entry) { + var entryNodes = this.data.getElementsByTagName("entry"); + array.forEach(entryNodes, function (entryNode) { + var entry = new Entry(entryNode); var item = new itemClass({entry: entry}); items.push(item); }); diff --git a/src/main/amp/web/js/softwareloop/inboxes/Item.js b/src/main/amp/web/js/softwareloop/inboxes/Item.js index 8a7a53f..15e4816 100644 --- a/src/main/amp/web/js/softwareloop/inboxes/Item.js +++ b/src/main/amp/web/js/softwareloop/inboxes/Item.js @@ -10,11 +10,8 @@ define([ "dojo/date/locale", "alfresco/dialogs/AlfDialog", "alfresco/core/Core", - "dojo/_base/lang", - "dojo/_base/array", - "softwareloop/util/browser", - "softwareloop/util/cmis" -], function (TemplatedMixin, WidgetBase, declare, template, locale, AlfDialog, Core, lang, array, browser, cmis) { + "dojo/_base/lang" +], function (TemplatedMixin, WidgetBase, declare, template, locale, AlfDialog, Core, lang) { return declare([WidgetBase, TemplatedMixin, Core], { templateString: template, @@ -28,8 +25,6 @@ define([ entry: null, - entryId: null, - entryAttributes: null, previewUrl: "", downloadUrl: "", @@ -44,71 +39,16 @@ define([ downloadLabel: "download", postMixInProperties: function () { - this.bindToEntry(); this.composeLines(); }, - bindToEntry: function () { - this.entryId = this.entry.getElementsByTagName("id")[0].firstChild.nodeValue.substring(9); - this.entryAttributes = {}; - this.parseProperties("propertyId", - function (stringValue) { - return stringValue; - } - ); - this.parseProperties("propertyString", - function (stringValue) { - return stringValue; - } - ); - this.parseProperties("propertyInteger", parseInt); - - this.parseProperties("propertyBoolean", - function (stringValue) { - return stringValue === 'true'; - } - ); - this.parseProperties("propertyDateTime", cmis.parseDate); - }, - - parseProperties: function (tagName, converter) { - var propertyStrings = - browser.getElementsByTagName(this.entry, "cmis", tagName); - for (var i = 0; i < propertyStrings.length; i++) { - var propertyString = propertyStrings[i]; - var cmisAttributeName = - propertyString.getAttribute("propertyDefinitionId"); - var entryAttribute = { - tagName: tagName, - values: [], - value: function () { - return (this.values.length > 0) ? this.values[0] : null - } - }; - this.entryAttributes[cmisAttributeName] = entryAttribute; - var valueNode = browser.getElementsByTagName(propertyString, "cmis", "value"); - if (valueNode) { - array.forEach(valueNode, function (current) { - var cmisAttributeValue = null; - try { - var nodeValue = current.firstChild.nodeValue; - cmisAttributeValue = converter(nodeValue); - } catch (e) { - cmisAttributeValue = null; - } - entryAttribute.values.push(cmisAttributeValue); - }); - } - } - }, - composeLines: function () { - if (this.entryAttributes["cmis:baseTypeId"].value() === "cmis:document") { + if (this.entry.getAttributeValue("cmis:baseTypeId") === "cmis:document") { this.previewUrl = lang.replace( "{proxyUri}api/node/workspace/SpacesStore/{entryId}/content/thumbnails/doclib?c=queue&ph=true&lastModified=1", { proxyUri: Alfresco.constants.PROXY_URI, - entryId: this.entryId + entryId: this.entry.id } ); } else { @@ -123,37 +63,37 @@ define([ "{proxyUri}api/node/content/workspace/SpacesStore/{entryId}/{filename}?a=true", { proxyUri: Alfresco.constants.PROXY_URI, - entryId: this.entryId, - filename: encodeURIComponent(this.entryAttributes["cmis:name"].value()) + entryId: this.entry.id, + filename: encodeURIComponent(this.entry.getAttributeValue("cmis:name")) } ); - this.escapedLine1 = this.encodeHTML(this.entryAttributes["cmis:name"].value()); + this.escapedLine1 = this.encodeHTML(this.entry.getAttributeValue("cmis:name")); if (!this.escapedLine1) { this.escapedLine1 = ""; } - this.escapedLine2 = this.encodeHTML(this.entryAttributes["cm:title"].value()); + this.escapedLine2 = this.encodeHTML(this.entry.getAttributeValue("cm:title")); if (!this.escapedLine2) { this.escapedLine2 = ""; } var line3 = this.message( "modified.on.by", { - date: locale.format(this.entryAttributes["cmis:lastModificationDate"].value(), { + date: locale.format(this.entry.getAttributeValue("cmis:lastModificationDate"), { formatLength: "medium", locale: Alfresco.constants.JS_LOCALE.substring(0, 2) }), - user: this.entryAttributes["cmis:lastModifiedBy"].value() + user: this.entry.getAttributeValue("cmis:lastModifiedBy") } ); this.escapedLine3 = this.encodeHTML(line3); if (!this.escapedLine3) { this.escapedLine3 = ""; } - this.escapedLine4 = this.encodeHTML(this.entryAttributes["cm:description"].value()); + this.escapedLine4 = this.encodeHTML(this.entry.getAttributeValue("cm:description")); if (!this.escapedLine4) { this.escapedLine4 = ""; } - var versionLabel = this.entryAttributes["cmis:versionLabel"].value(); + var versionLabel = this.entry.getAttributeValue("cmis:versionLabel"); if ("0.0" === versionLabel) { versionLabel = "1.0"; } @@ -165,7 +105,7 @@ define([ this.downloadLabel = this.message( "download.size", { - size: this.getHumanSize(this.entryAttributes["cmis:contentStreamLength"].value()) + size: this.getHumanSize(this.entry.getAttributeValue("cmis:contentStreamLength")) } ); }, @@ -185,7 +125,7 @@ define([ "Document approved", "This is just a stub action. " + "To provide a real implementation you should customize " + - "Item.approveAction()", + "Item.approveAction()", true ); }, @@ -195,7 +135,7 @@ define([ "Document rejected", "This is just a stub action. " + "To provide a real implementation you should customize " + - "Item.rejectAction()", + "Item.rejectAction()", true ); },