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