1
22
23 package com.liferay.portlet.journal.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.CharPool;
28 import com.liferay.portal.kernel.util.OrderByComparator;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.model.ResourceConstants;
33 import com.liferay.portal.model.User;
34 import com.liferay.portal.util.PortalUtil;
35 import com.liferay.portlet.journal.DuplicateStructureIdException;
36 import com.liferay.portlet.journal.NoSuchStructureException;
37 import com.liferay.portlet.journal.RequiredStructureException;
38 import com.liferay.portlet.journal.StructureDescriptionException;
39 import com.liferay.portlet.journal.StructureIdException;
40 import com.liferay.portlet.journal.StructureNameException;
41 import com.liferay.portlet.journal.StructureXsdException;
42 import com.liferay.portlet.journal.model.JournalStructure;
43 import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
44 import com.liferay.portlet.journal.service.base.JournalStructureLocalServiceBaseImpl;
45 import com.liferay.portlet.journal.util.JournalUtil;
46
47 import java.io.IOException;
48 import java.io.StringReader;
49
50 import java.util.Date;
51 import java.util.HashSet;
52 import java.util.List;
53 import java.util.Set;
54
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57
58 import org.dom4j.Document;
59 import org.dom4j.DocumentException;
60 import org.dom4j.Element;
61 import org.dom4j.io.SAXReader;
62
63
70 public class JournalStructureLocalServiceImpl
71 extends JournalStructureLocalServiceBaseImpl {
72
73 public JournalStructure addStructure(
74 long userId, String structureId, boolean autoStructureId, long plid,
75 String name, String description, String xsd,
76 boolean addCommunityPermissions, boolean addGuestPermissions)
77 throws PortalException, SystemException {
78
79 return addStructure(
80 null, userId, structureId, autoStructureId, plid, name, description,
81 xsd, Boolean.valueOf(addCommunityPermissions),
82 Boolean.valueOf(addGuestPermissions), null, null);
83 }
84
85 public JournalStructure addStructure(
86 String uuid, long userId, String structureId,
87 boolean autoStructureId, long plid, String name, String description,
88 String xsd, boolean addCommunityPermissions,
89 boolean addGuestPermissions)
90 throws PortalException, SystemException {
91
92 return addStructure(
93 uuid, userId, structureId, autoStructureId, plid, name, description,
94 xsd, Boolean.valueOf(addCommunityPermissions),
95 Boolean.valueOf(addGuestPermissions), null, null);
96 }
97
98 public JournalStructure addStructure(
99 long userId, String structureId, boolean autoStructureId, long plid,
100 String name, String description, String xsd,
101 String[] communityPermissions, String[] guestPermissions)
102 throws PortalException, SystemException {
103
104 return addStructure(
105 null, userId, structureId, autoStructureId, plid, name, description,
106 xsd, null, null, communityPermissions, guestPermissions);
107 }
108
109 public JournalStructure addStructure(
110 String uuid, long userId, String structureId,
111 boolean autoStructureId, long plid, String name,
112 String description, String xsd, Boolean addCommunityPermissions,
113 Boolean addGuestPermissions, String[] communityPermissions,
114 String[] guestPermissions)
115 throws PortalException, SystemException {
116
117 long groupId = PortalUtil.getPortletGroupId(plid);
118
119 return addStructureToGroup(
120 uuid, userId, structureId, autoStructureId, groupId, name,
121 description, xsd, addCommunityPermissions, addGuestPermissions,
122 communityPermissions, guestPermissions);
123 }
124
125 public JournalStructure addStructureToGroup(
126 String uuid, long userId, String structureId,
127 boolean autoStructureId, long groupId, String name,
128 String description, String xsd, Boolean addCommunityPermissions,
129 Boolean addGuestPermissions, String[] communityPermissions,
130 String[] guestPermissions)
131 throws PortalException, SystemException {
132
133
135 User user = userPersistence.findByPrimaryKey(userId);
136 structureId = structureId.trim().toUpperCase();
137 Date now = new Date();
138
139 try {
140 xsd = JournalUtil.formatXML(xsd);
141 }
142 catch (DocumentException de) {
143 throw new StructureXsdException();
144 }
145 catch (IOException ioe) {
146 throw new StructureXsdException();
147 }
148
149 validate(
150 groupId, structureId, autoStructureId, name, description, xsd);
151
152 if (autoStructureId) {
153 structureId = String.valueOf(counterLocalService.increment());
154 }
155
156 long id = counterLocalService.increment();
157
158 JournalStructure structure = journalStructurePersistence.create(id);
159
160 structure.setUuid(uuid);
161 structure.setGroupId(groupId);
162 structure.setCompanyId(user.getCompanyId());
163 structure.setUserId(user.getUserId());
164 structure.setUserName(user.getFullName());
165 structure.setCreateDate(now);
166 structure.setModifiedDate(now);
167 structure.setStructureId(structureId);
168 structure.setName(name);
169 structure.setDescription(description);
170 structure.setXsd(xsd);
171
172 journalStructurePersistence.update(structure, false);
173
174
176 if ((addCommunityPermissions != null) &&
177 (addGuestPermissions != null)) {
178
179 addStructureResources(
180 structure, addCommunityPermissions.booleanValue(),
181 addGuestPermissions.booleanValue());
182 }
183 else {
184 addStructureResources(
185 structure, communityPermissions, guestPermissions);
186 }
187
188 return structure;
189 }
190
191 public void addStructureResources(
192 long groupId, String structureId, boolean addCommunityPermissions,
193 boolean addGuestPermissions)
194 throws PortalException, SystemException {
195
196 JournalStructure structure = journalStructurePersistence.findByG_S(
197 groupId, structureId);
198
199 addStructureResources(
200 structure, addCommunityPermissions, addGuestPermissions);
201 }
202
203 public void addStructureResources(
204 JournalStructure structure, boolean addCommunityPermissions,
205 boolean addGuestPermissions)
206 throws PortalException, SystemException {
207
208 resourceLocalService.addResources(
209 structure.getCompanyId(), structure.getGroupId(),
210 structure.getUserId(), JournalStructure.class.getName(),
211 structure.getId(), false, addCommunityPermissions,
212 addGuestPermissions);
213 }
214
215 public void addStructureResources(
216 long groupId, String structureId, String[] communityPermissions,
217 String[] guestPermissions)
218 throws PortalException, SystemException {
219
220 JournalStructure structure = journalStructurePersistence.findByG_S(
221 groupId, structureId);
222
223 addStructureResources(
224 structure, communityPermissions, guestPermissions);
225 }
226
227 public void addStructureResources(
228 JournalStructure structure, String[] communityPermissions,
229 String[] guestPermissions)
230 throws PortalException, SystemException {
231
232 resourceLocalService.addModelResources(
233 structure.getCompanyId(), structure.getGroupId(),
234 structure.getUserId(), JournalStructure.class.getName(),
235 structure.getId(), communityPermissions, guestPermissions);
236 }
237
238 public void checkNewLine(long groupId, String structureId)
239 throws PortalException, SystemException {
240
241 JournalStructure structure = journalStructurePersistence.findByG_S(
242 groupId, structureId);
243
244 String xsd = structure.getXsd();
245
246 if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
247 xsd = StringUtil.replace(
248 xsd,
249 new String[] {"\\n", "\\r"},
250 new String[] {"\n", "\r"});
251
252 structure.setXsd(xsd);
253
254 journalStructurePersistence.update(structure, false);
255 }
256 }
257
258 public void deleteStructure(long groupId, String structureId)
259 throws PortalException, SystemException {
260
261 structureId = structureId.trim().toUpperCase();
262
263 JournalStructure structure = journalStructurePersistence.findByG_S(
264 groupId, structureId);
265
266 deleteStructure(structure);
267 }
268
269 public void deleteStructure(JournalStructure structure)
270 throws PortalException, SystemException {
271
272 if (journalArticlePersistence.countByG_S(
273 structure.getGroupId(), structure.getStructureId()) > 0) {
274
275 throw new RequiredStructureException();
276 }
277
278 if (journalTemplatePersistence.countByG_S(
279 structure.getGroupId(), structure.getStructureId()) > 0) {
280
281 throw new RequiredStructureException();
282 }
283
284
286 webDAVPropsLocalService.deleteWebDAVProps(
287 JournalStructure.class.getName(), structure.getPrimaryKey());
288
289
291 resourceLocalService.deleteResource(
292 structure.getCompanyId(), JournalStructure.class.getName(),
293 ResourceConstants.SCOPE_INDIVIDUAL, structure.getId());
294
295
297 journalStructurePersistence.remove(structure.getPrimaryKey());
298 }
299
300 public void deleteStructures(long groupId)
301 throws PortalException, SystemException {
302
303 for (JournalStructure structure :
304 journalStructurePersistence.findByGroupId(groupId)) {
305
306 deleteStructure(structure);
307 }
308 }
309
310 public JournalStructure getStructure(long id)
311 throws PortalException, SystemException {
312
313 return journalStructurePersistence.findByPrimaryKey(id);
314 }
315
316 public JournalStructure getStructure(long groupId, String structureId)
317 throws PortalException, SystemException {
318
319 structureId = structureId.trim().toUpperCase();
320
321 if (groupId == 0) {
322 _log.error(
323 "No group id was passed for " + structureId + ". Group id is " +
324 "required since 4.2.0. Please update all custom code and " +
325 "data that references structures without a group id.");
326
327 List<JournalStructure> structures =
328 journalStructurePersistence.findByStructureId(structureId);
329
330 if (structures.size() == 0) {
331 throw new NoSuchStructureException(
332 "No JournalStructure exists with the structure id " +
333 structureId);
334 }
335 else {
336 return structures.get(0);
337 }
338 }
339 else {
340 return journalStructurePersistence.findByG_S(groupId, structureId);
341 }
342 }
343
344 public List<JournalStructure> getStructures() throws SystemException {
345 return journalStructurePersistence.findAll();
346 }
347
348 public List<JournalStructure> getStructures(long groupId)
349 throws SystemException {
350
351 return journalStructurePersistence.findByGroupId(groupId);
352 }
353
354 public List<JournalStructure> getStructures(
355 long groupId, int start, int end)
356 throws SystemException {
357
358 return journalStructurePersistence.findByGroupId(groupId, start, end);
359 }
360
361 public int getStructuresCount(long groupId) throws SystemException {
362 return journalStructurePersistence.countByGroupId(groupId);
363 }
364
365 public List<JournalStructure> search(
366 long companyId, long groupId, String keywords, int start, int end,
367 OrderByComparator obc)
368 throws SystemException {
369
370 return journalStructureFinder.findByKeywords(
371 companyId, groupId, keywords, start, end, obc);
372 }
373
374 public List<JournalStructure> search(
375 long companyId, long groupId, String structureId, String name,
376 String description, boolean andOperator, int start, int end,
377 OrderByComparator obc)
378 throws SystemException {
379
380 return journalStructureFinder.findByC_G_S_N_D(
381 companyId, groupId, structureId, name, description, andOperator,
382 start, end, obc);
383 }
384
385 public int searchCount(long companyId, long groupId, String keywords)
386 throws SystemException {
387
388 return journalStructureFinder.countByKeywords(
389 companyId, groupId, keywords);
390 }
391
392 public int searchCount(
393 long companyId, long groupId, String structureId, String name,
394 String description, boolean andOperator)
395 throws SystemException {
396
397 return journalStructureFinder.countByC_G_S_N_D(
398 companyId, groupId, structureId, name, description, andOperator);
399 }
400
401 public JournalStructure updateStructure(
402 long groupId, String structureId, String name, String description,
403 String xsd)
404 throws PortalException, SystemException {
405
406 structureId = structureId.trim().toUpperCase();
407
408 try {
409 xsd = JournalUtil.formatXML(xsd);
410 }
411 catch (DocumentException de) {
412 throw new StructureXsdException();
413 }
414 catch (IOException ioe) {
415 throw new StructureXsdException();
416 }
417
418 validate(name, description, xsd);
419
420 JournalStructure structure = journalStructurePersistence.findByG_S(
421 groupId, structureId);
422
423 structure.setModifiedDate(new Date());
424 structure.setName(name);
425 structure.setDescription(description);
426 structure.setXsd(xsd);
427
428 journalStructurePersistence.update(structure, false);
429
430 return structure;
431 }
432
433 protected void validate(
434 long groupId, String structureId, boolean autoStructureId,
435 String name, String description, String xsd)
436 throws PortalException, SystemException {
437
438 if (!autoStructureId) {
439 if ((Validator.isNull(structureId)) ||
440 (Validator.isNumber(structureId)) ||
441 (structureId.indexOf(StringPool.SPACE) != -1)) {
442
443 throw new StructureIdException();
444 }
445
446 try {
447 journalStructurePersistence.findByG_S(groupId, structureId);
448
449 throw new DuplicateStructureIdException();
450 }
451 catch (NoSuchStructureException nste) {
452 }
453 }
454
455 validate(name, description, xsd);
456 }
457
458 protected void validate(String name, String description, String xsd)
459 throws PortalException {
460
461 if (Validator.isNull(name)) {
462 throw new StructureNameException();
463 }
464 else if (Validator.isNull(description)) {
465 throw new StructureDescriptionException();
466 }
467
468 if (Validator.isNull(xsd)) {
469 throw new StructureXsdException();
470 }
471 else {
472 try {
473 SAXReader reader = new SAXReader();
474
475 Document doc = reader.read(new StringReader(xsd));
476
477 Element root = doc.getRootElement();
478
479 List<Element> children = root.elements();
480
481 if (children.size() == 0) {
482 throw new StructureXsdException();
483 }
484
485 Set<String> elNames = new HashSet<String>();
486
487 validate(children, elNames);
488 }
489 catch (Exception e) {
490 throw new StructureXsdException();
491 }
492 }
493 }
494
495 protected void validate(List<Element> children, Set<String> elNames)
496 throws PortalException {
497
498 for (Element el : children) {
499 String elName = el.attributeValue("name", StringPool.BLANK);
500 String elType = el.attributeValue("type", StringPool.BLANK);
501
502 if (Validator.isNull(elName) ||
503 elName.startsWith(JournalStructureImpl.RESERVED)) {
504
505 throw new StructureXsdException();
506 }
507 else {
508 char[] c = elName.toCharArray();
509
510 for (int i = 0; i < c.length; i++) {
511 if ((!Validator.isChar(c[i])) &&
512 (!Validator.isDigit(c[i])) && (c[i] != CharPool.DASH) &&
513 (c[i] != CharPool.UNDERLINE)) {
514
515 throw new StructureXsdException();
516 }
517 }
518
519 String completePath = elName;
520
521 Element parent = el.getParent();
522
523 while (!parent.isRootElement()) {
524 completePath =
525 parent.attributeValue("name", StringPool.BLANK) +
526 StringPool.SLASH + completePath;
527
528 parent = parent.getParent();
529 }
530
531 String elNameLowerCase = completePath.toLowerCase();
532
533 if (elNames.contains(elNameLowerCase)) {
534 throw new StructureXsdException();
535 }
536 else {
537 elNames.add(elNameLowerCase);
538 }
539 }
540
541 if (Validator.isNull(elType)) {
542 throw new StructureXsdException();
543 }
544
545 validate(el.elements(), elNames);
546 }
547 }
548
549 private static Log _log =
550 LogFactory.getLog(JournalStructureLocalServiceImpl.class);
551
552 }