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.portal.service.impl;
016    
017    import com.liferay.portal.LayoutSetBranchNameException;
018    import com.liferay.portal.NoSuchLayoutSetBranchException;
019    import com.liferay.portal.RequiredLayoutSetBranchException;
020    import com.liferay.portal.kernel.dao.orm.QueryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.language.LanguageUtil;
024    import com.liferay.portal.kernel.staging.StagingUtil;
025    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.Image;
031    import com.liferay.portal.model.Layout;
032    import com.liferay.portal.model.LayoutBranch;
033    import com.liferay.portal.model.LayoutBranchConstants;
034    import com.liferay.portal.model.LayoutConstants;
035    import com.liferay.portal.model.LayoutRevision;
036    import com.liferay.portal.model.LayoutRevisionConstants;
037    import com.liferay.portal.model.LayoutSet;
038    import com.liferay.portal.model.LayoutSetBranch;
039    import com.liferay.portal.model.LayoutSetBranchConstants;
040    import com.liferay.portal.model.ResourceConstants;
041    import com.liferay.portal.model.User;
042    import com.liferay.portal.service.ServiceContext;
043    import com.liferay.portal.service.base.LayoutSetBranchLocalServiceBaseImpl;
044    import com.liferay.portal.util.comparator.LayoutSetBranchCreateDateComparator;
045    
046    import java.text.Format;
047    
048    import java.util.Date;
049    import java.util.List;
050    import java.util.Locale;
051    
052    /**
053     * @author Raymond Aug??
054     * @author Brian Wing Shun Chan
055     * @author Julio Camarero
056     */
057    public class LayoutSetBranchLocalServiceImpl
058            extends LayoutSetBranchLocalServiceBaseImpl {
059    
060            @Override
061            public LayoutSetBranch addLayoutSetBranch(
062                            long userId, long groupId, boolean privateLayout, String name,
063                            String description, boolean master, long copyLayoutSetBranchId,
064                            ServiceContext serviceContext)
065                    throws PortalException, SystemException {
066    
067                    // Layout branch
068    
069                    User user = userPersistence.findByPrimaryKey(userId);
070                    Date now = new Date();
071    
072                    validate(0, groupId, privateLayout, name, master);
073    
074                    boolean logo = false;
075                    long logoId = 0;
076                    String themeId = null;
077                    String colorSchemeId = null;
078                    String wapThemeId = null;
079                    String wapColorSchemeId = null;
080                    String css = null;
081                    String settings = null;
082    
083                    if (copyLayoutSetBranchId > 0) {
084                            LayoutSetBranch copyLayoutSetBranch = getLayoutSetBranch(
085                                    copyLayoutSetBranchId);
086    
087                            logo = copyLayoutSetBranch.getLogo();
088                            logoId = copyLayoutSetBranch.getLogoId();
089                            themeId = copyLayoutSetBranch.getThemeId();
090                            colorSchemeId = copyLayoutSetBranch.getColorSchemeId();
091                            wapThemeId = copyLayoutSetBranch.getWapThemeId();
092                            wapColorSchemeId = copyLayoutSetBranch.getWapColorSchemeId();
093                            css = copyLayoutSetBranch.getCss();
094                            settings = copyLayoutSetBranch.getSettings();
095                    }
096                    else {
097                            LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
098                                    groupId, privateLayout);
099    
100                            logo = layoutSet.getLogo();
101                            logoId = layoutSet.getLogoId();
102                            themeId = layoutSet.getThemeId();
103                            colorSchemeId = layoutSet.getColorSchemeId();
104                            wapThemeId = layoutSet.getWapThemeId();
105                            wapColorSchemeId = layoutSet.getWapColorSchemeId();
106                            css = layoutSet.getCss();
107                            settings = layoutSet.getSettings();
108                    }
109    
110                    long layoutSetBranchId = counterLocalService.increment();
111    
112                    LayoutSetBranch layoutSetBranch = layoutSetBranchPersistence.create(
113                            layoutSetBranchId);
114    
115                    layoutSetBranch.setGroupId(groupId);
116                    layoutSetBranch.setCompanyId(user.getCompanyId());
117                    layoutSetBranch.setUserId(user.getUserId());
118                    layoutSetBranch.setUserName(user.getFullName());
119                    layoutSetBranch.setCreateDate(serviceContext.getCreateDate(now));
120                    layoutSetBranch.setModifiedDate(serviceContext.getModifiedDate(now));
121                    layoutSetBranch.setPrivateLayout(privateLayout);
122                    layoutSetBranch.setName(name);
123                    layoutSetBranch.setDescription(description);
124                    layoutSetBranch.setMaster(master);
125                    layoutSetBranch.setLogo(logo);
126                    layoutSetBranch.setLogoId(logoId);
127    
128                    if (logo) {
129                            Image logoImage = imageLocalService.getImage(logoId);
130    
131                            long layoutSetBranchLogoId = counterLocalService.increment();
132    
133                            imageLocalService.updateImage(
134                                    layoutSetBranchLogoId, logoImage.getTextObj(),
135                                    logoImage.getType(), logoImage.getHeight(),
136                                    logoImage.getWidth(), logoImage.getSize());
137    
138                            layoutSetBranch.setLogoId(layoutSetBranchLogoId);
139                    }
140    
141                    layoutSetBranch.setThemeId(themeId);
142                    layoutSetBranch.setColorSchemeId(colorSchemeId);
143                    layoutSetBranch.setWapThemeId(wapThemeId);
144                    layoutSetBranch.setWapColorSchemeId(wapColorSchemeId);
145                    layoutSetBranch.setCss(css);
146                    layoutSetBranch.setSettings(settings);
147    
148                    layoutSetBranchPersistence.update(layoutSetBranch, false);
149    
150                    // Resources
151    
152                    resourceLocalService.addResources(
153                            user.getCompanyId(), layoutSetBranch.getGroupId(), user.getUserId(),
154                            LayoutSetBranch.class.getName(),
155                            layoutSetBranch.getLayoutSetBranchId(), false, true, false);
156    
157                    // Layout revisions
158    
159                    if (layoutSetBranch.isMaster() ||
160                            (copyLayoutSetBranchId == LayoutSetBranchConstants.ALL_BRANCHES)) {
161    
162                            List<Layout> layouts = layoutPersistence.findByG_P(
163                                    layoutSetBranch.getGroupId(),
164                                    layoutSetBranch.getPrivateLayout());
165    
166                            for (Layout layout : layouts) {
167                                    LayoutBranch layoutBranch =
168                                            layoutBranchLocalService.addLayoutBranch(
169                                                    layoutSetBranchId, layout.getPlid(),
170                                                    LayoutBranchConstants.MASTER_BRANCH_NAME,
171                                                    LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
172                                                    serviceContext);
173    
174                                    LayoutRevision lastLayoutRevision =
175                                            layoutRevisionLocalService.fetchLastLayoutRevision(
176                                                    layout.getPlid(), true);
177    
178                                    if (lastLayoutRevision != null) {
179                                            layoutRevisionLocalService.addLayoutRevision(
180                                                    userId, layoutSetBranchId,
181                                                    layoutBranch.getLayoutBranchId(),
182                                                    LayoutRevisionConstants.
183                                                            DEFAULT_PARENT_LAYOUT_REVISION_ID,
184                                                    true, lastLayoutRevision.getPlid(),
185                                                    lastLayoutRevision.getLayoutRevisionId(),
186                                                    lastLayoutRevision.getPrivateLayout(),
187                                                    lastLayoutRevision.getName(),
188                                                    lastLayoutRevision.getTitle(),
189                                                    lastLayoutRevision.getDescription(),
190                                                    lastLayoutRevision.getKeywords(),
191                                                    lastLayoutRevision.getRobots(),
192                                                    lastLayoutRevision.getTypeSettings(),
193                                                    lastLayoutRevision.isIconImage(),
194                                                    lastLayoutRevision.getIconImageId(),
195                                                    lastLayoutRevision.getThemeId(),
196                                                    lastLayoutRevision.getColorSchemeId(),
197                                                    lastLayoutRevision.getWapThemeId(),
198                                                    lastLayoutRevision.getWapColorSchemeId(),
199                                                    lastLayoutRevision.getCss(), serviceContext);
200                                    }
201                                    else {
202                                            layoutRevisionLocalService.addLayoutRevision(
203                                                    userId, layoutSetBranchId,
204                                                    layoutBranch.getLayoutBranchId(),
205                                                    LayoutRevisionConstants.
206                                                            DEFAULT_PARENT_LAYOUT_REVISION_ID,
207                                                    false, layout.getPlid(), LayoutConstants.DEFAULT_PLID,
208                                                    layout.getPrivateLayout(), layout.getName(),
209                                                    layout.getTitle(), layout.getDescription(),
210                                                    layout.getKeywords(), layout.getRobots(),
211                                                    layout.getTypeSettings(), layout.isIconImage(),
212                                                    layout.getIconImageId(), layout.getThemeId(),
213                                                    layout.getColorSchemeId(), layout.getWapThemeId(),
214                                                    layout.getWapColorSchemeId(), layout.getCss(),
215                                                    serviceContext);
216                                    }
217                            }
218                    }
219                    else if (copyLayoutSetBranchId > 0) {
220                            List<LayoutRevision> layoutRevisions =
221                                    layoutRevisionLocalService.getLayoutRevisions(
222                                            copyLayoutSetBranchId, true);
223    
224                            for (LayoutRevision layoutRevision : layoutRevisions) {
225                                    LayoutBranch layoutBranch =
226                                            layoutBranchLocalService.addLayoutBranch(
227                                                    layoutSetBranchId, layoutRevision.getPlid(),
228                                                    LayoutBranchConstants.MASTER_BRANCH_NAME,
229                                                    LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
230                                                    serviceContext);
231    
232                                    layoutRevisionLocalService.addLayoutRevision(
233                                            userId, layoutSetBranchId, layoutBranch.getLayoutBranchId(),
234                                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
235                                            true, layoutRevision.getPlid(),
236                                            layoutRevision.getLayoutRevisionId(),
237                                            layoutRevision.getPrivateLayout(), layoutRevision.getName(),
238                                            layoutRevision.getTitle(), layoutRevision.getDescription(),
239                                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
240                                            layoutRevision.getTypeSettings(),
241                                            layoutRevision.isIconImage(),
242                                            layoutRevision.getIconImageId(),
243                                            layoutRevision.getThemeId(),
244                                            layoutRevision.getColorSchemeId(),
245                                            layoutRevision.getWapThemeId(),
246                                            layoutRevision.getWapColorSchemeId(),
247                                            layoutRevision.getCss(), serviceContext);
248                            }
249                    }
250    
251                    return layoutSetBranch;
252            }
253    
254            @Override
255            public LayoutSetBranch deleteLayoutSetBranch(
256                            LayoutSetBranch layoutSetBranch)
257                    throws PortalException, SystemException {
258    
259                    return deleteLayoutSetBranch(layoutSetBranch, false);
260            }
261    
262            @Override
263            public LayoutSetBranch deleteLayoutSetBranch(
264                            LayoutSetBranch layoutSetBranch, boolean includeMaster)
265                    throws PortalException, SystemException {
266    
267                    // Layout branch
268    
269                    if (!includeMaster && layoutSetBranch.isMaster()) {
270                            throw new RequiredLayoutSetBranchException();
271                    }
272    
273                    layoutSetBranchPersistence.remove(layoutSetBranch);
274    
275                    // Resources
276    
277                    resourceLocalService.deleteResource(
278                            layoutSetBranch.getCompanyId(), LayoutSetBranch.class.getName(),
279                            ResourceConstants.SCOPE_INDIVIDUAL,
280                            layoutSetBranch.getLayoutSetBranchId());
281    
282                    // Layout branches
283    
284                    layoutBranchLocalService.deleteLayoutSetBranchLayoutBranches(
285                            layoutSetBranch.getLayoutSetBranchId());
286    
287                    // Layout revisions
288    
289                    layoutRevisionLocalService.deleteLayoutSetBranchLayoutRevisions(
290                            layoutSetBranch.getLayoutSetBranchId());
291    
292                    return layoutSetBranch;
293            }
294    
295            @Override
296            public LayoutSetBranch deleteLayoutSetBranch(long layoutSetBranchId)
297                    throws PortalException, SystemException {
298    
299                    LayoutSetBranch layoutSetBranch =
300                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
301    
302                    return deleteLayoutSetBranch(layoutSetBranch, false);
303            }
304    
305            @Override
306            public void deleteLayoutSetBranches(long groupId, boolean privateLayout)
307                    throws PortalException, SystemException {
308    
309                    deleteLayoutSetBranches(groupId, privateLayout, false);
310            }
311    
312            @Override
313            public void deleteLayoutSetBranches(
314                            long groupId, boolean privateLayout, boolean includeMaster)
315                    throws PortalException, SystemException {
316    
317                    List<LayoutSetBranch> layoutSetBranches =
318                            layoutSetBranchPersistence.findByG_P(groupId, privateLayout);
319    
320                    for (LayoutSetBranch layoutSetBranch : layoutSetBranches) {
321                            deleteLayoutSetBranch(layoutSetBranch, includeMaster);
322                    }
323            }
324    
325            @Override
326            public LayoutSetBranch fetchLayoutSetBranch(
327                            long groupId, boolean privateLayout, String name)
328                    throws SystemException {
329    
330                    return layoutSetBranchPersistence.fetchByG_P_N(
331                            groupId, privateLayout, name);
332            }
333    
334            @Override
335            public LayoutSetBranch getLayoutSetBranch(
336                            long groupId, boolean privateLayout, String name)
337                    throws PortalException, SystemException {
338    
339                    return layoutSetBranchPersistence.findByG_P_N(
340                            groupId, privateLayout, name);
341            }
342    
343            @Override
344            public List<LayoutSetBranch> getLayoutSetBranches(
345                            long groupId, boolean privateLayout)
346                    throws SystemException {
347    
348                    return layoutSetBranchPersistence.findByG_P(
349                            groupId, privateLayout, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
350                            new LayoutSetBranchCreateDateComparator(true));
351            }
352    
353            @Override
354            public LayoutSetBranch getMasterLayoutSetBranch(
355                            long groupId, boolean privateLayout)
356                    throws PortalException, SystemException {
357    
358                    return layoutSetBranchPersistence.findByG_P_M_First(
359                            groupId, privateLayout, true, null);
360            }
361    
362            /**
363             * @deprecated {@link #getUserLayoutSetBranch(long, long, boolean, long,
364             *             long)}
365             */
366            @Override
367            public LayoutSetBranch getUserLayoutSetBranch(
368                            long userId, long groupId, boolean privateLayout,
369                            long layoutSetBranchId)
370                    throws PortalException, SystemException {
371    
372                    return getUserLayoutSetBranch(
373                            userId, groupId, privateLayout, 0, layoutSetBranchId);
374            }
375    
376            @Override
377            public LayoutSetBranch getUserLayoutSetBranch(
378                            long userId, long groupId, boolean privateLayout, long layoutSetId,
379                            long layoutSetBranchId)
380                    throws PortalException, SystemException {
381    
382                    if (layoutSetBranchId <= 0) {
383                            User user = userPersistence.findByPrimaryKey(userId);
384    
385                            if (layoutSetId <= 0) {
386                                    LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
387                                            groupId, privateLayout);
388    
389                                    layoutSetId = layoutSet.getLayoutSetId();
390                            }
391    
392                            layoutSetBranchId = StagingUtil.getRecentLayoutSetBranchId(
393                                    user, layoutSetId);
394                    }
395    
396                    if (layoutSetBranchId > 0) {
397                            LayoutSetBranch layoutSetBranch = fetchLayoutSetBranch(
398                                    layoutSetBranchId);
399    
400                            if (layoutSetBranch != null) {
401                                    return layoutSetBranch;
402                            }
403                    }
404    
405                    return getMasterLayoutSetBranch(groupId, privateLayout);
406            }
407    
408            @Override
409            public LayoutSetBranch mergeLayoutSetBranch(
410                            long layoutSetBranchId, long mergeLayoutSetBranchId,
411                            ServiceContext serviceContext)
412                    throws PortalException, SystemException {
413    
414                    LayoutSetBranch layoutSetBranch =
415                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
416                    LayoutSetBranch mergeLayoutSetBranch =
417                            layoutSetBranchPersistence.findByPrimaryKey(mergeLayoutSetBranchId);
418    
419                    Locale locale = serviceContext.getLocale();
420    
421                    Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
422                            locale);
423    
424                    String nowString = dateFormatDateTime.format(new Date());
425    
426                    serviceContext.setWorkflowAction(WorkflowConstants.STATUS_DRAFT);
427    
428                    List<LayoutRevision> layoutRevisions =
429                            layoutRevisionLocalService.getLayoutRevisions(
430                                    mergeLayoutSetBranchId, true);
431    
432                    for (LayoutRevision layoutRevision : layoutRevisions) {
433                            String layoutBranchName = getLayoutBranchName(
434                                    layoutSetBranch.getLayoutSetBranchId(), locale,
435                                    layoutRevision.getLayoutBranch().getName(),
436                                    mergeLayoutSetBranch.getName(), layoutRevision.getPlid());
437    
438                            StringBundler sb = new StringBundler(3);
439    
440                            sb.append(mergeLayoutSetBranch.getDescription());
441                            sb.append(StringPool.SPACE);
442                            sb.append(
443                                    LanguageUtil.format(
444                                            locale, "merged-from-x-x",
445                                            new String[] {mergeLayoutSetBranch.getName(), nowString}));
446    
447                            LayoutBranch layoutBranch =
448                                    layoutBranchLocalService.addLayoutBranch(
449                                            layoutSetBranch.getLayoutSetBranchId(),
450                                            layoutRevision.getPlid(), layoutBranchName, sb.toString(),
451                                            false, serviceContext);
452    
453                            layoutRevisionLocalService.addLayoutRevision(
454                                    layoutRevision.getUserId(),
455                                    layoutSetBranch.getLayoutSetBranchId(),
456                                    layoutBranch.getLayoutBranchId(),
457                                    LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
458                                    false, layoutRevision.getPlid(),
459                                    layoutRevision.getLayoutRevisionId(),
460                                    layoutRevision.isPrivateLayout(), layoutRevision.getName(),
461                                    layoutRevision.getTitle(), layoutRevision.getDescription(),
462                                    layoutRevision.getKeywords(), layoutRevision.getRobots(),
463                                    layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
464                                    layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
465                                    layoutRevision.getColorSchemeId(),
466                                    layoutRevision.getWapThemeId(),
467                                    layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
468                                    serviceContext);
469                    }
470    
471                    return layoutSetBranch;
472            }
473    
474            @Override
475            public LayoutSetBranch updateLayoutSetBranch(
476                            long layoutSetBranchId, String name, String description,
477                            ServiceContext serviceContext)
478                    throws PortalException, SystemException {
479    
480                    LayoutSetBranch layoutSetBranch =
481                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
482    
483                    validate(
484                            layoutSetBranch.getLayoutSetBranchId(),
485                            layoutSetBranch.getGroupId(), layoutSetBranch.getPrivateLayout(),
486                            name, layoutSetBranch.isMaster());
487    
488                    layoutSetBranch.setName(name);
489                    layoutSetBranch.setDescription(description);
490    
491                    layoutSetBranchPersistence.update(layoutSetBranch, false);
492    
493                    return layoutSetBranch;
494            }
495    
496            protected String getLayoutBranchName(
497                            long layoutSetBranchId, Locale locale, String mergeBranchName,
498                            String mergeLayoutSetBranchName, long plid)
499                    throws SystemException {
500    
501                    LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_N(
502                            layoutSetBranchId, plid, mergeBranchName);
503    
504                    if (layoutBranch == null) {
505                            return mergeBranchName;
506                    }
507    
508                    StringBundler sb = new StringBundler(5);
509    
510                    sb.append(LanguageUtil.get(locale, mergeBranchName));
511                    sb.append(StringPool.SPACE);
512                    sb.append(StringPool.OPEN_PARENTHESIS);
513                    sb.append(LanguageUtil.get(locale, mergeLayoutSetBranchName));
514                    sb.append(StringPool.CLOSE_PARENTHESIS);
515    
516                    String layoutBranchName = sb.toString();
517    
518                    for (int i = 1;; i++) {
519                            layoutBranch = layoutBranchPersistence.fetchByL_P_N(
520                                    layoutSetBranchId, plid, layoutBranchName);
521    
522                            if (layoutBranch == null) {
523                                    break;
524                            }
525    
526                            layoutBranchName = sb.toString() + StringPool.SPACE + i;
527                    }
528    
529                    return layoutBranchName;
530            }
531    
532            protected void validate(
533                            long layoutSetBranchId, long groupId, boolean privateLayout,
534                            String name, boolean master)
535                    throws PortalException, SystemException {
536    
537                    if (Validator.isNull(name) || (name.length() < 4)) {
538                            throw new LayoutSetBranchNameException(
539                                    LayoutSetBranchNameException.TOO_SHORT);
540                    }
541    
542                    if (name.length() > 100) {
543                            throw new LayoutSetBranchNameException(
544                                    LayoutSetBranchNameException.TOO_LONG);
545                    }
546    
547                    try {
548                            LayoutSetBranch layoutSetBranch =
549                                    layoutSetBranchPersistence.findByG_P_N(
550                                            groupId, privateLayout, name);
551    
552                            if (layoutSetBranch.getLayoutSetBranchId() != layoutSetBranchId) {
553                                    throw new LayoutSetBranchNameException(
554                                            LayoutSetBranchNameException.DUPLICATE);
555                            }
556                    }
557                    catch (NoSuchLayoutSetBranchException nslsbe) {
558                    }
559    
560                    if (master) {
561                            try {
562                                    LayoutSetBranch masterLayoutSetBranch =
563                                            layoutSetBranchPersistence.findByG_P_M_First(
564                                                    groupId, privateLayout, true, null);
565    
566                                    if (layoutSetBranchId !=
567                                                    masterLayoutSetBranch.getLayoutSetBranchId()) {
568    
569                                            throw new LayoutSetBranchNameException(
570                                                    LayoutSetBranchNameException.MASTER);
571                                    }
572                            }
573                            catch (NoSuchLayoutSetBranchException nslsbe) {
574                            }
575                    }
576            }
577    
578    }