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