001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.OrderByComparator;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.User;
030: import com.liferay.portal.model.impl.ResourceImpl;
031: import com.liferay.portal.util.PortalUtil;
032: import com.liferay.portlet.journal.DuplicateStructureIdException;
033: import com.liferay.portlet.journal.NoSuchStructureException;
034: import com.liferay.portlet.journal.RequiredStructureException;
035: import com.liferay.portlet.journal.StructureDescriptionException;
036: import com.liferay.portlet.journal.StructureIdException;
037: import com.liferay.portlet.journal.StructureNameException;
038: import com.liferay.portlet.journal.StructureXsdException;
039: import com.liferay.portlet.journal.model.JournalStructure;
040: import com.liferay.portlet.journal.model.impl.JournalStructureImpl;
041: import com.liferay.portlet.journal.service.base.JournalStructureLocalServiceBaseImpl;
042: import com.liferay.portlet.journal.util.JournalUtil;
043:
044: import java.io.IOException;
045: import java.io.StringReader;
046:
047: import java.util.Date;
048: import java.util.HashSet;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Set;
052:
053: import org.apache.commons.logging.Log;
054: import org.apache.commons.logging.LogFactory;
055:
056: import org.dom4j.Document;
057: import org.dom4j.DocumentException;
058: import org.dom4j.Element;
059: import org.dom4j.io.SAXReader;
060:
061: /**
062: * <a href="JournalStructureLocalServiceImpl.java.html"><b><i>View Source</i>
063: * </b></a>
064: *
065: * @author Brian Wing Shun Chan
066: *
067: */
068: public class JournalStructureLocalServiceImpl extends
069: JournalStructureLocalServiceBaseImpl {
070:
071: public JournalStructure addStructure(long userId,
072: String structureId, boolean autoStructureId, long plid,
073: String name, String description, String xsd,
074: boolean addCommunityPermissions, boolean addGuestPermissions)
075: throws PortalException, SystemException {
076:
077: return addStructure(null, userId, structureId, autoStructureId,
078: plid, name, description, xsd, Boolean
079: .valueOf(addCommunityPermissions), Boolean
080: .valueOf(addGuestPermissions), null, null);
081: }
082:
083: public JournalStructure addStructure(String uuid, long userId,
084: String structureId, boolean autoStructureId, long plid,
085: String name, String description, String xsd,
086: boolean addCommunityPermissions, boolean addGuestPermissions)
087: throws PortalException, SystemException {
088:
089: return addStructure(uuid, userId, structureId, autoStructureId,
090: plid, name, description, xsd, Boolean
091: .valueOf(addCommunityPermissions), Boolean
092: .valueOf(addGuestPermissions), null, null);
093: }
094:
095: public JournalStructure addStructure(long userId,
096: String structureId, boolean autoStructureId, long plid,
097: String name, String description, String xsd,
098: String[] communityPermissions, String[] guestPermissions)
099: throws PortalException, SystemException {
100:
101: return addStructure(null, userId, structureId, autoStructureId,
102: plid, name, description, xsd, null, null,
103: communityPermissions, guestPermissions);
104: }
105:
106: public JournalStructure addStructure(String uuid, long userId,
107: String structureId, boolean autoStructureId, long plid,
108: String name, String description, String xsd,
109: Boolean addCommunityPermissions,
110: Boolean addGuestPermissions, String[] communityPermissions,
111: String[] guestPermissions) throws PortalException,
112: SystemException {
113:
114: long groupId = PortalUtil.getPortletGroupId(plid);
115:
116: return addStructureToGroup(uuid, userId, structureId,
117: autoStructureId, groupId, name, description, xsd,
118: addCommunityPermissions, addGuestPermissions,
119: communityPermissions, guestPermissions);
120: }
121:
122: public JournalStructure addStructureToGroup(String uuid,
123: long userId, String structureId, boolean autoStructureId,
124: long groupId, String name, String description, String xsd,
125: Boolean addCommunityPermissions,
126: Boolean addGuestPermissions, String[] communityPermissions,
127: String[] guestPermissions) throws PortalException,
128: SystemException {
129:
130: // Structure
131:
132: User user = userPersistence.findByPrimaryKey(userId);
133: structureId = structureId.trim().toUpperCase();
134: Date now = new Date();
135:
136: try {
137: xsd = JournalUtil.formatXML(xsd);
138: } catch (DocumentException de) {
139: throw new StructureXsdException();
140: } catch (IOException ioe) {
141: throw new StructureXsdException();
142: }
143:
144: validate(groupId, structureId, autoStructureId, name,
145: description, xsd);
146:
147: if (autoStructureId) {
148: structureId = String.valueOf(counterLocalService
149: .increment());
150: }
151:
152: long id = counterLocalService.increment();
153:
154: JournalStructure structure = journalStructurePersistence
155: .create(id);
156:
157: structure.setUuid(uuid);
158: structure.setGroupId(groupId);
159: structure.setCompanyId(user.getCompanyId());
160: structure.setUserId(user.getUserId());
161: structure.setUserName(user.getFullName());
162: structure.setCreateDate(now);
163: structure.setModifiedDate(now);
164: structure.setStructureId(structureId);
165: structure.setName(name);
166: structure.setDescription(description);
167: structure.setXsd(xsd);
168:
169: journalStructurePersistence.update(structure);
170:
171: // Resources
172:
173: if ((addCommunityPermissions != null)
174: && (addGuestPermissions != null)) {
175:
176: addStructureResources(structure, addCommunityPermissions
177: .booleanValue(), addGuestPermissions.booleanValue());
178: } else {
179: addStructureResources(structure, communityPermissions,
180: guestPermissions);
181: }
182:
183: return structure;
184: }
185:
186: public void addStructureResources(long groupId, String structureId,
187: boolean addCommunityPermissions, boolean addGuestPermissions)
188: throws PortalException, SystemException {
189:
190: JournalStructure structure = journalStructurePersistence
191: .findByG_S(groupId, structureId);
192:
193: addStructureResources(structure, addCommunityPermissions,
194: addGuestPermissions);
195: }
196:
197: public void addStructureResources(JournalStructure structure,
198: boolean addCommunityPermissions, boolean addGuestPermissions)
199: throws PortalException, SystemException {
200:
201: resourceLocalService.addResources(structure.getCompanyId(),
202: structure.getGroupId(), structure.getUserId(),
203: JournalStructure.class.getName(), structure.getId(),
204: false, addCommunityPermissions, addGuestPermissions);
205: }
206:
207: public void addStructureResources(long groupId, String structureId,
208: String[] communityPermissions, String[] guestPermissions)
209: throws PortalException, SystemException {
210:
211: JournalStructure structure = journalStructurePersistence
212: .findByG_S(groupId, structureId);
213:
214: addStructureResources(structure, communityPermissions,
215: guestPermissions);
216: }
217:
218: public void addStructureResources(JournalStructure structure,
219: String[] communityPermissions, String[] guestPermissions)
220: throws PortalException, SystemException {
221:
222: resourceLocalService.addModelResources(
223: structure.getCompanyId(), structure.getGroupId(),
224: structure.getUserId(),
225: JournalStructure.class.getName(), structure.getId(),
226: communityPermissions, guestPermissions);
227: }
228:
229: public void checkNewLine(long groupId, String structureId)
230: throws PortalException, SystemException {
231:
232: JournalStructure structure = journalStructurePersistence
233: .findByG_S(groupId, structureId);
234:
235: String xsd = structure.getXsd();
236:
237: if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
238: xsd = StringUtil.replace(xsd,
239: new String[] { "\\n", "\\r" }, new String[] { "\n",
240: "\r" });
241:
242: structure.setXsd(xsd);
243:
244: journalStructurePersistence.update(structure);
245: }
246: }
247:
248: public void deleteStructure(long groupId, String structureId)
249: throws PortalException, SystemException {
250:
251: structureId = structureId.trim().toUpperCase();
252:
253: JournalStructure structure = journalStructurePersistence
254: .findByG_S(groupId, structureId);
255:
256: deleteStructure(structure);
257: }
258:
259: public void deleteStructure(JournalStructure structure)
260: throws PortalException, SystemException {
261:
262: if (journalArticlePersistence.countByG_S(
263: structure.getGroupId(), structure.getStructureId()) > 0) {
264:
265: throw new RequiredStructureException();
266: }
267:
268: if (journalTemplatePersistence.countByG_S(structure
269: .getGroupId(), structure.getStructureId()) > 0) {
270:
271: throw new RequiredStructureException();
272: }
273:
274: // Resources
275:
276: resourceLocalService.deleteResource(structure.getCompanyId(),
277: JournalStructure.class.getName(),
278: ResourceImpl.SCOPE_INDIVIDUAL, structure.getId());
279:
280: // WebDAVProps
281:
282: webDAVPropsLocalService.deleteWebDAVProps(
283: JournalStructure.class.getName(), structure
284: .getPrimaryKey());
285:
286: // Structure
287:
288: journalStructurePersistence.remove(structure.getPrimaryKey());
289: }
290:
291: public void deleteStructures(long groupId) throws PortalException,
292: SystemException {
293:
294: Iterator itr = journalStructurePersistence.findByGroupId(
295: groupId).iterator();
296:
297: while (itr.hasNext()) {
298: JournalStructure structure = (JournalStructure) itr.next();
299:
300: deleteStructure(structure);
301: }
302: }
303:
304: public JournalStructure getStructure(long id)
305: throws PortalException, SystemException {
306:
307: return journalStructurePersistence.findByPrimaryKey(id);
308: }
309:
310: public JournalStructure getStructure(long groupId,
311: String structureId) throws PortalException, SystemException {
312:
313: structureId = structureId.trim().toUpperCase();
314:
315: if (groupId == 0) {
316: _log
317: .error("No group id was passed for "
318: + structureId
319: + ". Group id is "
320: + "required since 4.2.0. Please update all custom code and "
321: + "data that references structures without a group id.");
322:
323: List structures = journalStructurePersistence
324: .findByStructureId(structureId);
325:
326: if (structures.size() == 0) {
327: throw new NoSuchStructureException(
328: "No JournalStructure exists with the structure id "
329: + structureId);
330: } else {
331: return (JournalStructure) structures.get(0);
332: }
333: } else {
334: return journalStructurePersistence.findByG_S(groupId,
335: structureId);
336: }
337: }
338:
339: public List getStructures() throws SystemException {
340: return journalStructurePersistence.findAll();
341: }
342:
343: public List getStructures(long groupId) throws SystemException {
344: return journalStructurePersistence.findByGroupId(groupId);
345: }
346:
347: public List getStructures(long groupId, int begin, int end)
348: throws SystemException {
349:
350: return journalStructurePersistence.findByGroupId(groupId,
351: begin, end);
352: }
353:
354: public int getStructuresCount(long groupId) throws SystemException {
355: return journalStructurePersistence.countByGroupId(groupId);
356: }
357:
358: public List search(long companyId, long groupId, String keywords,
359: int begin, int end, OrderByComparator obc)
360: throws SystemException {
361:
362: return journalStructureFinder.findByKeywords(companyId,
363: groupId, keywords, begin, end, obc);
364: }
365:
366: public List search(long companyId, long groupId,
367: String structureId, String name, String description,
368: boolean andOperator, int begin, int end,
369: OrderByComparator obc) throws SystemException {
370:
371: return journalStructureFinder.findByC_G_S_N_D(companyId,
372: groupId, structureId, name, description, andOperator,
373: begin, end, obc);
374: }
375:
376: public int searchCount(long companyId, long groupId, String keywords)
377: throws SystemException {
378:
379: return journalStructureFinder.countByKeywords(companyId,
380: groupId, keywords);
381: }
382:
383: public int searchCount(long companyId, long groupId,
384: String structureId, String name, String description,
385: boolean andOperator) throws SystemException {
386:
387: return journalStructureFinder.countByC_G_S_N_D(companyId,
388: groupId, structureId, name, description, andOperator);
389: }
390:
391: public JournalStructure updateStructure(long groupId,
392: String structureId, String name, String description,
393: String xsd) throws PortalException, SystemException {
394:
395: structureId = structureId.trim().toUpperCase();
396:
397: try {
398: xsd = JournalUtil.formatXML(xsd);
399: } catch (DocumentException de) {
400: throw new StructureXsdException();
401: } catch (IOException ioe) {
402: throw new StructureXsdException();
403: }
404:
405: validate(name, description, xsd);
406:
407: JournalStructure structure = journalStructurePersistence
408: .findByG_S(groupId, structureId);
409:
410: structure.setModifiedDate(new Date());
411: structure.setName(name);
412: structure.setDescription(description);
413: structure.setXsd(xsd);
414:
415: journalStructurePersistence.update(structure);
416:
417: return structure;
418: }
419:
420: protected void validate(long groupId, String structureId,
421: boolean autoStructureId, String name, String description,
422: String xsd) throws PortalException, SystemException {
423:
424: if (!autoStructureId) {
425: if ((Validator.isNull(structureId))
426: || (Validator.isNumber(structureId))
427: || (structureId.indexOf(StringPool.SPACE) != -1)) {
428:
429: throw new StructureIdException();
430: }
431:
432: try {
433: journalStructurePersistence.findByG_S(groupId,
434: structureId);
435:
436: throw new DuplicateStructureIdException();
437: } catch (NoSuchStructureException nste) {
438: }
439: }
440:
441: validate(name, description, xsd);
442: }
443:
444: protected void validate(String name, String description, String xsd)
445: throws PortalException {
446:
447: if (Validator.isNull(name)) {
448: throw new StructureNameException();
449: } else if (Validator.isNull(description)) {
450: throw new StructureDescriptionException();
451: }
452:
453: if (Validator.isNull(xsd)) {
454: throw new StructureXsdException();
455: } else {
456: try {
457: SAXReader reader = new SAXReader();
458:
459: Document doc = reader.read(new StringReader(xsd));
460:
461: Element root = doc.getRootElement();
462:
463: List children = root.elements();
464:
465: if (children.size() == 0) {
466: throw new StructureXsdException();
467: }
468:
469: Set elNames = new HashSet();
470:
471: validate(children, elNames);
472: } catch (Exception e) {
473: throw new StructureXsdException();
474: }
475: }
476: }
477:
478: protected void validate(List children, Set elNames)
479: throws PortalException {
480: Iterator itr = children.iterator();
481:
482: while (itr.hasNext()) {
483: Element el = (Element) itr.next();
484:
485: String elName = el.attributeValue("name", StringPool.BLANK);
486: String elType = el.attributeValue("type", StringPool.BLANK);
487:
488: if (Validator.isNull(elName)
489: || elName.startsWith(JournalStructureImpl.RESERVED)) {
490:
491: throw new StructureXsdException();
492: } else {
493: char[] c = elName.toCharArray();
494:
495: for (int i = 0; i < c.length; i++) {
496: if ((!Validator.isChar(c[i]))
497: && (!Validator.isDigit(c[i]))
498: && (c[i] != '_') && (c[i] != '-')) {
499:
500: throw new StructureXsdException();
501: }
502: }
503:
504: String completePath = elName;
505:
506: Element parent = el.getParent();
507:
508: while (!parent.isRootElement()) {
509: completePath = parent.attributeValue("name",
510: StringPool.BLANK)
511: + StringPool.SLASH + completePath;
512:
513: parent = parent.getParent();
514: }
515:
516: String elNameLowerCase = completePath.toLowerCase();
517:
518: if (elNames.contains(elNameLowerCase)) {
519: throw new StructureXsdException();
520: } else {
521: elNames.add(elNameLowerCase);
522: }
523: }
524:
525: if (Validator.isNull(elType)) {
526: throw new StructureXsdException();
527: }
528:
529: validate(el.elements(), elNames);
530: }
531: }
532:
533: private static Log _log = LogFactory
534: .getLog(JournalStructureLocalServiceImpl.class);
535:
536: }
|