001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portlet.dynamicdatamapping.StructureXsdException;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
034    import com.liferay.portlet.journal.DuplicateStructureIdException;
035    import com.liferay.portlet.journal.NoSuchStructureException;
036    import com.liferay.portlet.journal.StructureIdException;
037    import com.liferay.portlet.journal.model.JournalArticle;
038    import com.liferay.portlet.journal.model.JournalStructure;
039    import com.liferay.portlet.journal.model.JournalStructureAdapter;
040    import com.liferay.portlet.journal.service.base.JournalStructureLocalServiceBaseImpl;
041    import com.liferay.portlet.journal.util.JournalConverterUtil;
042    import com.liferay.portlet.journal.util.JournalUtil;
043    
044    import java.util.Date;
045    import java.util.List;
046    import java.util.Locale;
047    import java.util.Map;
048    
049    /**
050     * @author     Brian Wing Shun Chan
051     * @author     Raymond Aug??
052     * @author     Marcellus Tavares
053     * @deprecated As of 6.2.0, since Web Content Administration now uses the
054     *             Dynamic Data Mapping framework to handle structures
055     */
056    public class JournalStructureLocalServiceImpl
057            extends JournalStructureLocalServiceBaseImpl {
058    
059            @Override
060            public JournalStructure addJournalStructure(JournalStructure structure)
061                    throws PortalException, SystemException {
062    
063                    structure.setNew(true);
064    
065                    return updateStructure(structure);
066            }
067    
068            @Override
069            public JournalStructure addStructure(
070                            long userId, long groupId, String structureId,
071                            boolean autoStructureId, String parentStructureId,
072                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
073                            String xsd, ServiceContext serviceContext)
074                    throws PortalException, SystemException {
075    
076                    try {
077                            xsd = JournalConverterUtil.getDDMXSD(xsd);
078                    }
079                    catch (Exception e) {
080                            throw new StructureXsdException(e);
081                    }
082    
083                    if (autoStructureId) {
084                            structureId = null;
085                    }
086    
087                    DDMStructure ddmStructure = ddmStructureLocalService.addStructure(
088                            userId, groupId, parentStructureId,
089                            PortalUtil.getClassNameId(JournalArticle.class), structureId,
090                            nameMap, descriptionMap, xsd,
091                            PropsValues.JOURNAL_ARTICLE_STORAGE_TYPE,
092                            DDMStructureConstants.TYPE_DEFAULT, serviceContext);
093    
094                    return new JournalStructureAdapter(ddmStructure);
095            }
096    
097            @Override
098            public void addStructureResources(
099                            JournalStructure structure, boolean addGroupPermissions,
100                            boolean addGuestPermissions)
101                    throws PortalException, SystemException {
102    
103                    DDMStructure dmmStructure = getDDMStructure(structure);
104    
105                    ddmStructureLocalService.addStructureResources(
106                            dmmStructure, addGroupPermissions, addGuestPermissions);
107            }
108    
109            @Override
110            public void addStructureResources(
111                            JournalStructure structure, String[] groupPermissions,
112                            String[] guestPermissions)
113                    throws PortalException, SystemException {
114    
115                    DDMStructure dmmStructure = getDDMStructure(structure);
116    
117                    ddmStructureLocalService.addStructureResources(
118                            dmmStructure, groupPermissions, guestPermissions);
119            }
120    
121            @Override
122            public void addStructureResources(
123                            long groupId, String structureId, boolean addGroupPermissions,
124                            boolean addGuestPermissions)
125                    throws PortalException, SystemException {
126    
127                    JournalStructure structure = doGetStructure(groupId, structureId);
128    
129                    addStructureResources(
130                            structure, addGroupPermissions, addGuestPermissions);
131            }
132    
133            @Override
134            public void addStructureResources(
135                            long groupId, String structureId, String[] groupPermissions,
136                            String[] guestPermissions)
137                    throws PortalException, SystemException {
138    
139                    JournalStructure structure = doGetStructure(groupId, structureId);
140    
141                    addStructureResources(structure, groupPermissions, guestPermissions);
142            }
143    
144            @Override
145            public void checkNewLine(long groupId, String structureId)
146                    throws PortalException, SystemException {
147    
148                    JournalStructure structure = doGetStructure(groupId, structureId);
149    
150                    String xsd = structure.getXsd();
151    
152                    if ((xsd != null) && xsd.contains("\\n")) {
153                            xsd = StringUtil.replace(
154                                    xsd, new String[] {"\\n", "\\r"}, new String[] {"\n", "\r"});
155    
156                            structure.setXsd(xsd);
157    
158                            updateStructure(structure);
159                    }
160            }
161    
162            @Override
163            public JournalStructure copyStructure(
164                            long userId, long groupId, String oldStructureId,
165                            String newStructureId, boolean autoStructureId)
166                    throws PortalException, SystemException {
167    
168                    // Structure
169    
170                    User user = userPersistence.findByPrimaryKey(userId);
171                    oldStructureId = StringUtil.toUpperCase(oldStructureId.trim());
172                    newStructureId = StringUtil.toUpperCase(newStructureId.trim());
173                    Date now = new Date();
174    
175                    JournalStructure oldStructure = doGetStructure(groupId, oldStructureId);
176    
177                    if (autoStructureId) {
178                            newStructureId = String.valueOf(counterLocalService.increment());
179                    }
180                    else {
181                            validateStructureId(newStructureId);
182    
183                            JournalStructure newStructure = fetchStructure(
184                                    groupId, newStructureId);
185    
186                            if (newStructure != null) {
187                                    StringBundler sb = new StringBundler(5);
188    
189                                    sb.append("{groupId=");
190                                    sb.append(groupId);
191                                    sb.append(", structureId=");
192                                    sb.append(newStructureId);
193                                    sb.append("}");
194    
195                                    throw new DuplicateStructureIdException(sb.toString());
196                            }
197                    }
198    
199                    long id = counterLocalService.increment();
200    
201                    JournalStructure newStructure = createJournalStructure(id);
202    
203                    newStructure.setGroupId(groupId);
204                    newStructure.setCompanyId(user.getCompanyId());
205                    newStructure.setUserId(user.getUserId());
206                    newStructure.setUserName(user.getFullName());
207                    newStructure.setCreateDate(now);
208                    newStructure.setModifiedDate(now);
209                    newStructure.setStructureId(newStructureId);
210                    newStructure.setNameMap(oldStructure.getNameMap());
211                    newStructure.setDescriptionMap(oldStructure.getDescriptionMap());
212                    newStructure.setXsd(oldStructure.getXsd());
213                    newStructure.setExpandoBridgeAttributes(oldStructure);
214    
215                    return updateStructure(newStructure);
216            }
217    
218            @Override
219            public JournalStructure createJournalStructure(long id)
220                    throws SystemException {
221    
222                    DDMStructure ddmStructure = ddmStructureLocalService.createDDMStructure(
223                            id);
224    
225                    return new JournalStructureAdapter(ddmStructure);
226            }
227    
228            @Override
229            public void deleteStructure(JournalStructure structure)
230                    throws PortalException, SystemException {
231    
232                    DDMStructure ddmStructure = getDDMStructure(structure);
233    
234                    ddmStructureLocalService.deleteDDMStructure(ddmStructure);
235            }
236    
237            @Override
238            public void deleteStructure(long groupId, String structureId)
239                    throws PortalException, SystemException {
240    
241                    JournalStructure structure = doGetStructure(groupId, structureId);
242    
243                    deleteStructure(structure);
244            }
245    
246            @Override
247            public void deleteStructures(long groupId)
248                    throws PortalException, SystemException {
249    
250                    List<JournalStructure> structures = doGetStructures(
251                            groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
252    
253                    for (JournalStructure structure : structures) {
254                            deleteStructure(structure);
255                    }
256            }
257    
258            @Override
259            public JournalStructure fetchStructure(long groupId, String structureId)
260                    throws SystemException {
261    
262                    DDMStructure ddmStructure = fetchDDMStructure(groupId, structureId);
263    
264                    if (ddmStructure != null) {
265                            return new JournalStructureAdapter(ddmStructure);
266                    }
267    
268                    return null;
269            }
270    
271            @Override
272            public List<JournalStructure> findAll() throws SystemException {
273                    List<DDMStructure> ddmStructures =
274                            ddmStructureLocalService.getClassStructures(
275                                    PortalUtil.getClassNameId(JournalArticle.class));
276    
277                    return JournalUtil.toJournalStructures(ddmStructures);
278            }
279    
280            @Override
281            public JournalStructure getStructure(long groupId, String structureId)
282                    throws PortalException, SystemException {
283    
284                    return getStructure(groupId, structureId, false);
285            }
286    
287            @Override
288            public JournalStructure getStructure(
289                            long groupId, String structureId, boolean includeGlobalStructures)
290                    throws PortalException, SystemException {
291    
292                    structureId = StringUtil.toUpperCase(structureId.trim());
293    
294                    JournalStructure structure = fetchStructure(groupId, structureId);
295    
296                    if (structure != null) {
297                            return structure;
298                    }
299    
300                    if (!includeGlobalStructures) {
301                            throw new NoSuchStructureException(
302                                    "No JournalStructure exists with the structure ID " +
303                                            structureId);
304                    }
305    
306                    Group group = groupPersistence.findByPrimaryKey(groupId);
307    
308                    Group companyGroup = groupLocalService.getCompanyGroup(
309                            group.getCompanyId());
310    
311                    return doGetStructure(companyGroup.getGroupId(), structureId);
312            }
313    
314            @Override
315            public List<JournalStructure> getStructures() throws SystemException {
316                    return findAll();
317            }
318    
319            @Override
320            public List<JournalStructure> getStructures(long groupId)
321                    throws SystemException {
322    
323                    return doGetStructures(groupId);
324            }
325    
326            @Override
327            public List<JournalStructure> getStructures(
328                            long groupId, int start, int end)
329                    throws SystemException {
330    
331                    return doGetStructures(groupId, start, end);
332            }
333    
334            @Override
335            public int getStructuresCount(long groupId) throws SystemException {
336                    return doGetStructuresCount(groupId);
337            }
338    
339            @Override
340            public List<JournalStructure> search(
341                            long companyId, long[] groupIds, String keywords, int start,
342                            int end, OrderByComparator obc)
343                    throws SystemException {
344    
345                    long[] classNameIds = {PortalUtil.getClassNameId(JournalArticle.class)};
346    
347                    List<DDMStructure> ddmStructures = ddmStructureFinder.findByKeywords(
348                            companyId, groupIds, classNameIds, keywords, start, end, obc);
349    
350                    return JournalUtil.toJournalStructures(ddmStructures);
351            }
352    
353            @Override
354            public List<JournalStructure> search(
355                            long companyId, long[] groupIds, String structureId, String name,
356                            String description, boolean andOperator, int start, int end,
357                            OrderByComparator obc)
358                    throws SystemException {
359    
360                    long[] classNameIds = {PortalUtil.getClassNameId(JournalArticle.class)};
361    
362                    List<DDMStructure> ddmStructures =
363                            ddmStructureFinder.findByC_G_C_N_D_S_T(
364                                    companyId, groupIds, classNameIds, name, description, null,
365                                    DDMStructureConstants.TYPE_DEFAULT, andOperator, start, end,
366                                    obc);
367    
368                    return JournalUtil.toJournalStructures(ddmStructures);
369            }
370    
371            @Override
372            public int searchCount(long companyId, long[] groupIds, String keywords)
373                    throws SystemException {
374    
375                    long[] classNameIds = {PortalUtil.getClassNameId(JournalArticle.class)};
376    
377                    return ddmStructureFinder.countByKeywords(
378                            companyId, groupIds, classNameIds, keywords);
379            }
380    
381            @Override
382            public int searchCount(
383                            long companyId, long[] groupIds, String structureId, String name,
384                            String description, boolean andOperator)
385                    throws SystemException {
386    
387                    long[] classNameIds = {PortalUtil.getClassNameId(JournalArticle.class)};
388    
389                    return ddmStructureFinder.countByC_G_C_N_D_S_T(
390                            companyId, groupIds, classNameIds, name, description, null,
391                            DDMStructureConstants.TYPE_DEFAULT, andOperator);
392            }
393    
394            @Override
395            public JournalStructure updateJournalStructure(JournalStructure structure)
396                    throws PortalException, SystemException {
397    
398                    return updateStructure(structure);
399            }
400    
401            @Override
402            public JournalStructure updateStructure(
403                            long groupId, String structureId, String parentStructureId,
404                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
405                            String xsd, ServiceContext serviceContext)
406                    throws PortalException, SystemException {
407    
408                    DDMStructure ddmStructure = getDDMStructure(groupId, structureId);
409    
410                    long parentDDMStructureId = getParentDDMStructureId(
411                            groupId, parentStructureId);
412    
413                    try {
414                            xsd = JournalConverterUtil.getDDMXSD(xsd);
415                    }
416                    catch (Exception e) {
417                            throw new StructureXsdException(e);
418                    }
419    
420                    ddmStructure = ddmStructureLocalService.updateStructure(
421                            ddmStructure.getStructureId(), parentDDMStructureId, nameMap,
422                            descriptionMap, xsd, serviceContext);
423    
424                    return new JournalStructureAdapter(ddmStructure);
425            }
426    
427            protected JournalStructure doGetStructure(long groupId, String structureId)
428                    throws PortalException, SystemException {
429    
430                    DDMStructure ddmStructure = getDDMStructure(groupId, structureId);
431    
432                    return new JournalStructureAdapter(ddmStructure);
433            }
434    
435            protected List<JournalStructure> doGetStructures(long groupId)
436                    throws SystemException {
437    
438                    return doGetStructures(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
439            }
440    
441            protected List<JournalStructure> doGetStructures(
442                            long groupId, int start, int end)
443                    throws SystemException {
444    
445                    List<DDMStructure> ddmStructures =
446                            ddmStructureLocalService.getStructures(
447                                    groupId, PortalUtil.getClassNameId(JournalArticle.class), start,
448                                    end);
449    
450                    return JournalUtil.toJournalStructures(ddmStructures);
451            }
452    
453            protected int doGetStructuresCount(long groupId) throws SystemException {
454                    return ddmStructureLocalService.getStructuresCount(
455                            groupId, PortalUtil.getClassNameId(JournalArticle.class));
456            }
457    
458            protected DDMStructure fetchDDMStructure(JournalStructure structure)
459                    throws SystemException {
460    
461                    return fetchDDMStructure(
462                            structure.getGroupId(), structure.getStructureId());
463            }
464    
465            protected DDMStructure fetchDDMStructure(long groupId, String structureId)
466                    throws SystemException {
467    
468                    return ddmStructureLocalService.fetchStructure(
469                            groupId, PortalUtil.getClassNameId(JournalArticle.class),
470                            structureId);
471            }
472    
473            protected DDMStructure getDDMStructure(JournalStructure structure)
474                    throws PortalException, SystemException {
475    
476                    return getDDMStructure(
477                            structure.getGroupId(), structure.getStructureId());
478            }
479    
480            protected DDMStructure getDDMStructure(long groupId, String structureId)
481                    throws PortalException, SystemException {
482    
483                    try {
484                            return ddmStructureLocalService.getStructure(
485                                    groupId, PortalUtil.getClassNameId(JournalArticle.class),
486                                    structureId);
487                    }
488                    catch (PortalException pe) {
489                            throw new NoSuchStructureException(pe);
490                    }
491            }
492    
493            protected long getParentDDMStructureId(
494                            long groupId, String parentStructureId)
495                    throws SystemException {
496    
497                    DDMStructure parentDDMStructure = fetchDDMStructure(
498                            groupId, parentStructureId);
499    
500                    if (parentDDMStructure != null) {
501                            return parentDDMStructure.getStructureId();
502                    }
503    
504                    return 0;
505            }
506    
507            protected String getUuid(JournalStructure structure) {
508                    String uuid = structure.getUuid();
509    
510                    if (Validator.isNotNull(uuid)) {
511                            return uuid;
512                    }
513    
514                    return PortalUUIDUtil.generate();
515            }
516    
517            protected JournalStructure updateStructure(JournalStructure structure)
518                    throws PortalException, SystemException {
519    
520                    ServiceContext serviceContext = new ServiceContext();
521    
522                    serviceContext.setAddGroupPermissions(true);
523                    serviceContext.setAddGuestPermissions(true);
524    
525                    String uuid = getUuid(structure);
526    
527                    serviceContext.setUuid(uuid);
528    
529                    if (structure.isNew()) {
530                            return addStructure(
531                                    structure.getUserId(), structure.getGroupId(),
532                                    structure.getStructureId(), false,
533                                    structure.getParentStructureId(), structure.getNameMap(),
534                                    structure.getDescriptionMap(), structure.getXsd(),
535                                    serviceContext);
536                    }
537    
538                    return updateStructure(
539                            structure.getGroupId(), structure.getStructureId(),
540                            structure.getParentStructureId(), structure.getNameMap(),
541                            structure.getDescriptionMap(), structure.getXsd(), serviceContext);
542            }
543    
544            protected void validateStructureId(String structureId)
545                    throws PortalException {
546    
547                    if (Validator.isNull(structureId) ||
548                            Validator.isNumber(structureId) ||
549                            (structureId.indexOf(CharPool.COMMA) != -1) ||
550                            (structureId.indexOf(CharPool.SPACE) != -1)) {
551    
552                            throw new StructureIdException();
553                    }
554            }
555    
556    }