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