This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliferay-service-provider.js
206 lines (190 loc) · 6.24 KB
/
liferay-service-provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"use strict";
const TYPES = {
WEBCONTENT:0,
DDLRECORD:1
};
const SERVICE_URLS = {
BASE: "/api/jsonws",
CREATE_JOURNAL: "/journalarticle/add-article",
READ_JOURNAL: "/journalarticle/get-article",
READ_ALL_JOURNAL: "/journalarticle/get-articles",
UPDATE_JOURNAL: "/journalarticle/update-article",
DELETE_JOURNAL: "/journalarticle/delete-article",
CREATE_DDLRECORD: "/ddlrecord/add-record",
UPDATE_DDLRECORD: "/ddlrecord/update-record"
};
const BASE_JOURNAL_DATA = {
classNameId: 0,
classPK: 0,
articleId: "",
autoArticleId: true,
type: "general",
layoutUuid: "",
displayDateMonth: 1,
displayDateDay: 1,
displayDateYear: (new Date()).getFullYear(),
displayDateHour: 0,
displayDateMinute: 0,
expirationDateMonth: 1,
expirationDateDay: 1,
expirationDateYear: (new Date()).getFullYear(),
expirationDateHour: 0,
expirationDateMinute: 0,
neverExpire: true,
reviewDateMonth: 1,
reviewDateDay: 1,
reviewDateYear: (new Date()).getFullYear(),
reviewDateHour: 0,
reviewDateMinute: 0,
neverReview: true,
indexable: true,
articleURL: ""
};
angular.module("LiferayService", [])
.provider("liferay", function () {
var types = [];
var token = "";
return {
setToken: function (authtoken) {
token = authtoken;
},
addTypes: function (type) {
types.push(type);
},
$get: ["$http", "$q", function ($http, $q) {
return createObject($http, $q);
}]
};
function isWebContentType(type){
return (type != undefined ? type : TYPES.WEBCONTENT) === TYPES.WEBCONTENT;
}
function createObject($http, $q) {
var serviceObject = {
makeCall: function (urlpart, data) {
console.info(SERVICE_URLS.BASE + urlpart);
var helperPromise = $q.defer();
var postPromise = $http.post(SERVICE_URLS.BASE + urlpart, data, {
headers: { "Content-Type": "application/x-www-form-urlencoded", "Authorization": "Basic " + token },
transformRequest: function (obj) {
var formData = [];
for (var property in obj) {
formData.push(encodeURIComponent(property) + "=" + encodeURIComponent(obj[property]));
}
return formData.join("&");
}
}
).success(function (data) {
console.info(data);
console.info("data", arguments);
helperPromise.resolve(data);
}).error(function (message) {
console.info("message", message);
helperPromise.reject(message);
});
return helperPromise.promise;
},
createJournal: function (type, entry) {
var newEntry = {};
Object.assign(newEntry, BASE_JOURNAL_DATA, this._generateJournalEntry(type, entry));
return this.makeCall(SERVICE_URLS.CREATE_JOURNAL, newEntry);
},
getJournal: function (type, id) {
var result;
if (id !== undefined) {
result = this.makeCall(SERVICE_URLS.READ_JOURNAL, { groupId: type.groupId, articleId: id });
} else {
result = this.makeCall(SERVICE_URLS.READ_ALL_JOURNAL, { groupId: type.groupId, folderId: type.folderId });
}
return result;
},
updateJournal: function (type, entry, articleid) {
var newEntry = {};
Object.assign(newEntry, BASE_JOURNAL_DATA, this._generateJournalEntry(type, entry));
newEntry["articleId"] = articleid;
return this.makeCall(SERVICE_URLS.UPDATE_JOURNAL, newEntry);
},
deleteJournal: function (type, id) {
return this.makeCall(SERVICE_URLS.DELETE_JOURNAL, { groupId: type.groupId, articleId: id, articleURL: "" });
},
createDDLRecord: function (type, entry) {
var record = {};
record["groupId"] = type.groupId;
record["recordSetId"] = type.recordSetId;
record["displayIndex"] = 0;
record["fieldsMap"] = JSON.stringify(entry);
return this.makeCall(SERVICE_URLS.CREATE_DDLRECORD, record);
},
updateDDLRecord: function (type, entry, id) {
var record = {};
record["recordId"] = id;
record["mergeFields"] = true;
record["displayIndex"] = 0;
record["fieldsMap"] = JSON.stringify(entry);
return this.makeCall(SERVICE_URLS.UPDATE_DDLRECORD, record);
},
_generateJournalContent: function (entry) {
var innerData = "";
for (var prop in entry) {
if (entry.hasOwnProperty(prop)) {
innerData +=
"<dynamic-element name='" + prop + "' type='text' index-type='keyword' index='0'>" +
"<dynamic-content language-id='en_US'><![CDATA[" + entry[prop] + "]]></dynamic-content>" +
"</dynamic-element>";
}
}
return "<?xml version='1.0'?><root available-locales='en_US' default-locale='en_US'>" + innerData + "</root>";
},
_generateJournalEntry: function (type, entry) {
return {
groupId: type.groupId,
folderId: type.folderId,
content: this._generateJournalContent(entry),
titleMap: "{\"en_US\": \"Entry " + (new Date()).getTime() + "\"}",
descriptionMap: "{\"en_US\": \"Auto generated\"}",
ddmStructureKey: type.structureId,
ddmTemplateKey: type.templateId
};
},
_generateJournalJson: function (entry) {
var expression = /name="([^"]+)"(?:.|\s)+?<!\[CDATA\[(.+)\]\]>/gi;
var newObj = { _data: entry };
var data;
while (data = expression.exec(entry.content)) {
newObj[data[1]] = data[2];
}
return newObj;
}
};
types.forEach(function (value) {
serviceObject[value.name] = {
create: function (entry) {
return isWebContentType(value.type)?serviceObject.createJournal(value, entry):serviceObject.createDDLRecord(value, entry);
},
update: function (entry, id) {
return isWebContentType(value.type)?serviceObject.updateJournal(value, entry, id):serviceObject.updateDDLRecord(value, entry, id);
},
get: function (articleid) {
if(value.type === TYPES.DDLRECORD){
throw "Not Implemented";
}
return serviceObject.getJournal(value, articleid).then(function (data) {
var result;
if (articleid !== undefined) {
result = serviceObject._generateJournalJson(data.data);
} else {
result = data.data.map(serviceObject._generateJournalJson);
}
return result;
});
},
delete: function (articleid) {
if(value.type === TYPES.DDLRECORD){
throw "Not Implemented";
}
return serviceObject.deleteJournal(value, articleid);
}
};
});
return serviceObject;
}
});