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.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.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                    return layoutSetBranch;
295            }
296    
297            @Override
298            public LayoutSetBranch deleteLayoutSetBranch(long layoutSetBranchId)
299                    throws PortalException {
300    
301                    LayoutSetBranch layoutSetBranch =
302                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
303    
304                    return deleteLayoutSetBranch(layoutSetBranch, false);
305            }
306    
307            @Override
308            public void deleteLayoutSetBranches(long groupId, boolean privateLayout)
309                    throws PortalException {
310    
311                    deleteLayoutSetBranches(groupId, privateLayout, false);
312            }
313    
314            @Override
315            public void deleteLayoutSetBranches(
316                            long groupId, boolean privateLayout, boolean includeMaster)
317                    throws PortalException {
318    
319                    List<LayoutSetBranch> layoutSetBranches =
320                            layoutSetBranchPersistence.findByG_P(groupId, privateLayout);
321    
322                    for (LayoutSetBranch layoutSetBranch : layoutSetBranches) {
323                            deleteLayoutSetBranch(layoutSetBranch, includeMaster);
324                    }
325            }
326    
327            @Override
328            public LayoutSetBranch fetchLayoutSetBranch(
329                    long groupId, boolean privateLayout, String name) {
330    
331                    return layoutSetBranchPersistence.fetchByG_P_N(
332                            groupId, privateLayout, name);
333            }
334    
335            @Override
336            public LayoutSetBranch getLayoutSetBranch(
337                            long groupId, boolean privateLayout, String name)
338                    throws PortalException {
339    
340                    return layoutSetBranchPersistence.findByG_P_N(
341                            groupId, privateLayout, name);
342            }
343    
344            @Override
345            public List<LayoutSetBranch> getLayoutSetBranches(
346                    long groupId, boolean privateLayout) {
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 {
357    
358                    return layoutSetBranchPersistence.findByG_P_M_First(
359                            groupId, privateLayout, true, null);
360            }
361    
362            /**
363             * @deprecated As of 6.2.0, replaced by {@link #getUserLayoutSetBranch(long,
364             *             long, boolean, long, long)}
365             */
366            @Deprecated
367            @Override
368            public LayoutSetBranch getUserLayoutSetBranch(
369                            long userId, long groupId, boolean privateLayout,
370                            long layoutSetBranchId)
371                    throws PortalException {
372    
373                    return getUserLayoutSetBranch(
374                            userId, groupId, privateLayout, 0, layoutSetBranchId);
375            }
376    
377            @Override
378            public LayoutSetBranch getUserLayoutSetBranch(
379                            long userId, long groupId, boolean privateLayout, long layoutSetId,
380                            long layoutSetBranchId)
381                    throws PortalException {
382    
383                    if (layoutSetBranchId <= 0) {
384                            User user = userPersistence.findByPrimaryKey(userId);
385    
386                            if (layoutSetId <= 0) {
387                                    LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
388                                            groupId, privateLayout);
389    
390                                    layoutSetId = layoutSet.getLayoutSetId();
391                            }
392    
393                            layoutSetBranchId = StagingUtil.getRecentLayoutSetBranchId(
394                                    user, layoutSetId);
395                    }
396    
397                    if (layoutSetBranchId > 0) {
398                            LayoutSetBranch layoutSetBranch = fetchLayoutSetBranch(
399                                    layoutSetBranchId);
400    
401                            if (layoutSetBranch != null) {
402                                    return layoutSetBranch;
403                            }
404                    }
405    
406                    return getMasterLayoutSetBranch(groupId, privateLayout);
407            }
408    
409            @Override
410            public LayoutSetBranch mergeLayoutSetBranch(
411                            long layoutSetBranchId, long mergeLayoutSetBranchId,
412                            ServiceContext serviceContext)
413                    throws PortalException {
414    
415                    LayoutSetBranch layoutSetBranch =
416                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
417                    LayoutSetBranch mergeLayoutSetBranch =
418                            layoutSetBranchPersistence.findByPrimaryKey(mergeLayoutSetBranchId);
419    
420                    Locale locale = serviceContext.getLocale();
421    
422                    Format dateFormatDateTime = FastDateFormatFactoryUtil.getDateTime(
423                            locale);
424    
425                    String nowString = dateFormatDateTime.format(new Date());
426    
427                    serviceContext.setWorkflowAction(WorkflowConstants.STATUS_DRAFT);
428    
429                    List<LayoutRevision> layoutRevisions =
430                            layoutRevisionLocalService.getLayoutRevisions(
431                                    mergeLayoutSetBranchId, true);
432    
433                    for (LayoutRevision layoutRevision : layoutRevisions) {
434                            String layoutBranchName = getLayoutBranchName(
435                                    layoutSetBranch.getLayoutSetBranchId(), locale,
436                                    layoutRevision.getLayoutBranch().getName(),
437                                    mergeLayoutSetBranch.getName(), layoutRevision.getPlid());
438    
439                            StringBundler sb = new StringBundler(3);
440    
441                            sb.append(mergeLayoutSetBranch.getDescription());
442                            sb.append(StringPool.SPACE);
443                            sb.append(
444                                    LanguageUtil.format(
445                                            locale, "merged-from-x-x",
446                                            new String[] {mergeLayoutSetBranch.getName(), nowString},
447                                            false));
448    
449                            LayoutBranch layoutBranch =
450                                    layoutBranchLocalService.addLayoutBranch(
451                                            layoutSetBranch.getLayoutSetBranchId(),
452                                            layoutRevision.getPlid(), layoutBranchName, sb.toString(),
453                                            false, serviceContext);
454    
455                            layoutRevisionLocalService.addLayoutRevision(
456                                    layoutRevision.getUserId(),
457                                    layoutSetBranch.getLayoutSetBranchId(),
458                                    layoutBranch.getLayoutBranchId(),
459                                    LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
460                                    false, layoutRevision.getPlid(),
461                                    layoutRevision.getLayoutRevisionId(),
462                                    layoutRevision.isPrivateLayout(), layoutRevision.getName(),
463                                    layoutRevision.getTitle(), layoutRevision.getDescription(),
464                                    layoutRevision.getKeywords(), layoutRevision.getRobots(),
465                                    layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
466                                    layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
467                                    layoutRevision.getColorSchemeId(),
468                                    layoutRevision.getWapThemeId(),
469                                    layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
470                                    serviceContext);
471                    }
472    
473                    return layoutSetBranch;
474            }
475    
476            @Override
477            public LayoutSetBranch updateLayoutSetBranch(
478                            long layoutSetBranchId, String name, String description,
479                            ServiceContext serviceContext)
480                    throws PortalException {
481    
482                    LayoutSetBranch layoutSetBranch =
483                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
484    
485                    validate(
486                            layoutSetBranch.getLayoutSetBranchId(),
487                            layoutSetBranch.getGroupId(), layoutSetBranch.getPrivateLayout(),
488                            name, layoutSetBranch.isMaster());
489    
490                    layoutSetBranch.setName(name);
491                    layoutSetBranch.setDescription(description);
492    
493                    layoutSetBranchPersistence.update(layoutSetBranch);
494    
495                    return layoutSetBranch;
496            }
497    
498            protected String getLayoutBranchName(
499                    long layoutSetBranchId, Locale locale, String mergeBranchName,
500                    String mergeLayoutSetBranchName, long plid) {
501    
502                    LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_N(
503                            layoutSetBranchId, plid, mergeBranchName);
504    
505                    if (layoutBranch == null) {
506                            return mergeBranchName;
507                    }
508    
509                    String defaultLayoutBranchName = StringUtil.appendParentheticalSuffix(
510                            LanguageUtil.get(locale, mergeBranchName),
511                            LanguageUtil.get(locale, mergeLayoutSetBranchName));
512    
513                    String layoutBranchName = defaultLayoutBranchName;
514    
515                    for (int i = 1;; i++) {
516                            layoutBranch = layoutBranchPersistence.fetchByL_P_N(
517                                    layoutSetBranchId, plid, layoutBranchName);
518    
519                            if (layoutBranch == null) {
520                                    break;
521                            }
522    
523                            layoutBranchName = defaultLayoutBranchName + StringPool.SPACE + i;
524                    }
525    
526                    return layoutBranchName;
527            }
528    
529            protected void validate(
530                            long layoutSetBranchId, long groupId, boolean privateLayout,
531                            String name, boolean master)
532                    throws PortalException {
533    
534                    if (Validator.isNull(name) || (name.length() < 4)) {
535                            throw new LayoutSetBranchNameException(
536                                    LayoutSetBranchNameException.TOO_SHORT);
537                    }
538    
539                    if (name.length() > 100) {
540                            throw new LayoutSetBranchNameException(
541                                    LayoutSetBranchNameException.TOO_LONG);
542                    }
543    
544                    try {
545                            LayoutSetBranch layoutSetBranch =
546                                    layoutSetBranchPersistence.findByG_P_N(
547                                            groupId, privateLayout, name);
548    
549                            if (layoutSetBranch.getLayoutSetBranchId() != layoutSetBranchId) {
550                                    throw new LayoutSetBranchNameException(
551                                            LayoutSetBranchNameException.DUPLICATE);
552                            }
553                    }
554                    catch (NoSuchLayoutSetBranchException nslsbe) {
555                    }
556    
557                    if (master) {
558                            try {
559                                    LayoutSetBranch masterLayoutSetBranch =
560                                            layoutSetBranchPersistence.findByG_P_M_First(
561                                                    groupId, privateLayout, true, null);
562    
563                                    if (layoutSetBranchId !=
564                                                    masterLayoutSetBranch.getLayoutSetBranchId()) {
565    
566                                            throw new LayoutSetBranchNameException(
567                                                    LayoutSetBranchNameException.MASTER);
568                                    }
569                            }
570                            catch (NoSuchLayoutSetBranchException nslsbe) {
571                            }
572                    }
573            }
574    
575    }