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