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.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.staging.StagingUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.LayoutBranch;
027    import com.liferay.portal.model.LayoutBranchConstants;
028    import com.liferay.portal.model.LayoutConstants;
029    import com.liferay.portal.model.LayoutRevision;
030    import com.liferay.portal.model.LayoutRevisionConstants;
031    import com.liferay.portal.model.LayoutSet;
032    import com.liferay.portal.model.LayoutSetBranch;
033    import com.liferay.portal.model.LayoutSetBranchConstants;
034    import com.liferay.portal.model.ResourceConstants;
035    import com.liferay.portal.model.User;
036    import com.liferay.portal.service.ServiceContext;
037    import com.liferay.portal.service.base.LayoutSetBranchLocalServiceBaseImpl;
038    
039    import java.util.Date;
040    import java.util.List;
041    
042    /**
043     * @author Raymond Augé
044     * @author Brian Wing Shun Chan
045     */
046    public class LayoutSetBranchLocalServiceImpl
047            extends LayoutSetBranchLocalServiceBaseImpl {
048    
049            public LayoutSetBranch addLayoutSetBranch(
050                            long userId, long groupId, boolean privateLayout, String name,
051                            String description, boolean master, long copyLayoutSetBranchId,
052                            ServiceContext serviceContext)
053                    throws PortalException, SystemException {
054    
055                    // Layout branch
056    
057                    User user = userPersistence.findByPrimaryKey(userId);
058                    Date now = new Date();
059    
060                    validate(0, groupId, privateLayout, name, master);
061    
062                    long layoutSetBranchId = counterLocalService.increment();
063    
064                    LayoutSetBranch layoutSetBranch = layoutSetBranchPersistence.create(
065                            layoutSetBranchId);
066    
067                    layoutSetBranch.setGroupId(groupId);
068                    layoutSetBranch.setCompanyId(user.getCompanyId());
069                    layoutSetBranch.setUserId(user.getUserId());
070                    layoutSetBranch.setUserName(user.getFullName());
071                    layoutSetBranch.setCreateDate(serviceContext.getCreateDate(now));
072                    layoutSetBranch.setModifiedDate(serviceContext.getModifiedDate(now));
073                    layoutSetBranch.setPrivateLayout(privateLayout);
074                    layoutSetBranch.setName(name);
075                    layoutSetBranch.setDescription(description);
076                    layoutSetBranch.setMaster(master);
077    
078                    layoutSetBranchPersistence.update(layoutSetBranch, false);
079    
080                    // Resources
081    
082                    resourceLocalService.addResources(
083                            user.getCompanyId(), layoutSetBranch.getGroupId(), user.getUserId(),
084                            LayoutSetBranch.class.getName(),
085                            layoutSetBranch.getLayoutSetBranchId(), false, true, false);
086    
087                    // Layout revisions
088    
089                    if (layoutSetBranch.isMaster() ||
090                            (copyLayoutSetBranchId == LayoutSetBranchConstants.ALL_BRANCHES)) {
091    
092                            List<Layout> layouts = layoutPersistence.findByG_P(
093                                    layoutSetBranch.getGroupId(),
094                                    layoutSetBranch.getPrivateLayout());
095    
096                            for (Layout layout : layouts) {
097                                    LayoutBranch layoutBranch =
098                                            layoutBranchLocalService.addLayoutBranch(
099                                                    layoutSetBranchId, layout.getPlid(),
100                                                    LayoutBranchConstants.MASTER_BRANCH_NAME,
101                                                    LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
102                                                    serviceContext);
103    
104                                    LayoutRevision lastLayoutRevision =
105                                            layoutRevisionLocalService.fetchLastLayoutRevision(
106                                                    layout.getPlid(), true);
107    
108                                    if (lastLayoutRevision != null) {
109                                            layoutRevisionLocalService.addLayoutRevision(
110                                                    userId, layoutSetBranchId,
111                                                    layoutBranch.getLayoutBranchId(),
112                                                    LayoutRevisionConstants.
113                                                            DEFAULT_PARENT_LAYOUT_REVISION_ID,
114                                                    true, lastLayoutRevision.getPlid(),
115                                                    lastLayoutRevision.getLayoutRevisionId(),
116                                                    lastLayoutRevision.getPrivateLayout(),
117                                                    lastLayoutRevision.getName(),
118                                                    lastLayoutRevision.getTitle(),
119                                                    lastLayoutRevision.getDescription(),
120                                                    lastLayoutRevision.getKeywords(),
121                                                    lastLayoutRevision.getRobots(),
122                                                    lastLayoutRevision.getTypeSettings(),
123                                                    lastLayoutRevision.isIconImage(),
124                                                    lastLayoutRevision.getIconImageId(),
125                                                    lastLayoutRevision.getThemeId(),
126                                                    lastLayoutRevision.getColorSchemeId(),
127                                                    lastLayoutRevision.getWapThemeId(),
128                                                    lastLayoutRevision.getWapColorSchemeId(),
129                                                    lastLayoutRevision.getCss(), serviceContext);
130                                    }
131                                    else {
132                                            layoutRevisionLocalService.addLayoutRevision(
133                                                    userId, layoutSetBranchId,
134                                                    layoutBranch.getLayoutBranchId(),
135                                                    LayoutRevisionConstants.
136                                                            DEFAULT_PARENT_LAYOUT_REVISION_ID,
137                                                    false, layout.getPlid(), LayoutConstants.DEFAULT_PLID,
138                                                    layout.getPrivateLayout(), layout.getName(),
139                                                    layout.getTitle(), layout.getDescription(),
140                                                    layout.getKeywords(), layout.getRobots(),
141                                                    layout.getTypeSettings(), layout.isIconImage(),
142                                                    layout.getIconImageId(), layout.getThemeId(),
143                                                    layout.getColorSchemeId(), layout.getWapThemeId(),
144                                                    layout.getWapColorSchemeId(), layout.getCss(),
145                                                    serviceContext);
146                                    }
147                            }
148                    }
149                    else if (copyLayoutSetBranchId > 0) {
150                            List<LayoutRevision> layoutRevisions =
151                                    layoutRevisionLocalService.getLayoutRevisions(
152                                            copyLayoutSetBranchId, true);
153    
154                            for (LayoutRevision layoutRevision : layoutRevisions) {
155                                    LayoutBranch layoutBranch =
156                                            layoutBranchLocalService.addLayoutBranch(
157                                                    layoutSetBranchId, layoutRevision.getPlid(),
158                                                    LayoutBranchConstants.MASTER_BRANCH_NAME,
159                                                    LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
160                                                    serviceContext);
161    
162                                    layoutRevisionLocalService.addLayoutRevision(
163                                            userId, layoutSetBranchId,
164                                            layoutBranch.getLayoutBranchId(),
165                                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
166                                            true, layoutRevision.getPlid(),
167                                            layoutRevision.getLayoutRevisionId(),
168                                            layoutRevision.getPrivateLayout(), layoutRevision.getName(),
169                                            layoutRevision.getTitle(), layoutRevision.getDescription(),
170                                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
171                                            layoutRevision.getTypeSettings(),
172                                            layoutRevision.isIconImage(),
173                                            layoutRevision.getIconImageId(),
174                                            layoutRevision.getThemeId(),
175                                            layoutRevision.getColorSchemeId(),
176                                            layoutRevision.getWapThemeId(),
177                                            layoutRevision.getWapColorSchemeId(),
178                                            layoutRevision.getCss(), serviceContext);
179                            }
180                    }
181    
182                    return layoutSetBranch;
183            }
184    
185            @Override
186            public void deleteLayoutSetBranch(LayoutSetBranch layoutSetBranch)
187                    throws PortalException, SystemException {
188    
189                    deleteLayoutSetBranch(layoutSetBranch, false);
190            }
191    
192            public void deleteLayoutSetBranch(
193                            LayoutSetBranch layoutSetBranch, boolean includeMaster)
194                    throws PortalException, SystemException {
195    
196                    // Layout branch
197    
198                    if (!includeMaster && layoutSetBranch.isMaster()) {
199                            throw new RequiredLayoutSetBranchException();
200                    }
201    
202                    layoutSetBranchPersistence.remove(layoutSetBranch);
203    
204                    // Resources
205    
206                    resourceLocalService.deleteResource(
207                            layoutSetBranch.getCompanyId(), LayoutSetBranch.class.getName(),
208                            ResourceConstants.SCOPE_INDIVIDUAL,
209                            layoutSetBranch.getLayoutSetBranchId());
210    
211                    // Layout branches
212    
213                    layoutBranchLocalService.deleteLayoutSetBranchLayoutBranches(
214                            layoutSetBranch.getLayoutSetBranchId());
215    
216                    // Layout revisions
217    
218                    layoutRevisionLocalService.deleteLayoutSetBranchLayoutRevisions(
219                            layoutSetBranch.getLayoutSetBranchId());
220            }
221    
222            @Override
223            public void deleteLayoutSetBranch(long layoutSetBranchId)
224                    throws PortalException, SystemException {
225    
226                    LayoutSetBranch layoutSetBranch =
227                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
228    
229                    deleteLayoutSetBranch(layoutSetBranch);
230            }
231    
232            public void deleteLayoutSetBranches(long groupId, boolean privateLayout)
233                    throws PortalException, SystemException {
234    
235                    deleteLayoutSetBranches(groupId, privateLayout, false);
236            }
237    
238            public void deleteLayoutSetBranches(
239                            long groupId, boolean privateLayout, boolean includeMaster)
240                    throws PortalException, SystemException {
241    
242                    List<LayoutSetBranch> layoutSetBranches =
243                            layoutSetBranchPersistence.findByG_P(groupId, privateLayout);
244    
245                    for (LayoutSetBranch layoutSetBranch : layoutSetBranches) {
246                            deleteLayoutSetBranch(layoutSetBranch, includeMaster);
247                    }
248            }
249    
250            public LayoutSetBranch getLayoutSetBranch(
251                            long groupId, boolean privateLayout, String name)
252                    throws PortalException, SystemException {
253    
254                    return layoutSetBranchPersistence.findByG_P_N(
255                            groupId, privateLayout, name);
256            }
257    
258            public List<LayoutSetBranch> getLayoutSetBranches(
259                            long groupId, boolean privateLayout)
260                    throws SystemException {
261    
262                    return layoutSetBranchPersistence.findByG_P(groupId, privateLayout);
263            }
264    
265            public LayoutSetBranch getMasterLayoutSetBranch(
266                            long groupId, boolean privateLayout)
267                    throws PortalException, SystemException {
268    
269                    return layoutSetBranchFinder.findByMaster(groupId, privateLayout);
270            }
271    
272            public LayoutSetBranch getUserLayoutSetBranch(
273                            long userId, long groupId, boolean privateLayout,
274                            long layoutSetBranchId)
275                    throws PortalException, SystemException {
276    
277                    if (layoutSetBranchId <= 0) {
278                            User user = userPersistence.findByPrimaryKey(userId);
279    
280                            LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
281                                    groupId, privateLayout);
282    
283                            layoutSetBranchId = StagingUtil.getRecentLayoutSetBranchId(
284                                    user, layoutSet.getLayoutSetId());
285                    }
286    
287                    if (layoutSetBranchId > 0) {
288                            try {
289                                    return getLayoutSetBranch(layoutSetBranchId);
290                            }
291                            catch (NoSuchLayoutSetBranchException nslsbe) {
292                            }
293                    }
294    
295                    return getMasterLayoutSetBranch(groupId, privateLayout);
296            }
297    
298            public LayoutSetBranch mergeLayoutSetBranch(
299                            long layoutSetBranchId, long mergeLayoutSetBranchId,
300                            ServiceContext serviceContext)
301                    throws PortalException, SystemException {
302    
303                    LayoutSetBranch layoutSetBranch =
304                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
305    
306                    List<LayoutRevision> layoutRevisions =
307                            layoutRevisionLocalService.getLayoutRevisions(
308                                    mergeLayoutSetBranchId, true);
309    
310                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
311    
312                    for (LayoutRevision layoutRevision : layoutRevisions) {
313                            layoutRevisionLocalService.addLayoutRevision(
314                                    layoutRevision.getUserId(),
315                                    layoutSetBranch.getLayoutSetBranchId(),
316                                    layoutRevision.getLayoutBranchId(),
317                                    LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID,
318                                    false, layoutRevision.getPlid(),
319                                    layoutRevision.getLayoutRevisionId(),
320                                    layoutRevision.isPrivateLayout(), layoutRevision.getName(),
321                                    layoutRevision.getTitle(), layoutRevision.getDescription(),
322                                    layoutRevision.getKeywords(), layoutRevision.getRobots(),
323                                    layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
324                                    layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
325                                    layoutRevision.getColorSchemeId(),
326                                    layoutRevision.getWapThemeId(),
327                                    layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
328                                    serviceContext);
329                    }
330    
331                    return layoutSetBranch;
332            }
333    
334            public LayoutSetBranch updateLayoutSetBranch(
335                            long layoutSetBranchId, String name, String description,
336                            ServiceContext serviceContext)
337                    throws PortalException, SystemException {
338    
339                    LayoutSetBranch layoutSetBranch =
340                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
341    
342                    validate(
343                            layoutSetBranch.getLayoutSetBranchId(),
344                            layoutSetBranch.getGroupId(), layoutSetBranch.getPrivateLayout(),
345                            name, layoutSetBranch.isMaster());
346    
347                    layoutSetBranch.setName(name);
348                    layoutSetBranch.setDescription(description);
349    
350                    layoutSetBranchPersistence.update(layoutSetBranch, false);
351    
352                    return layoutSetBranch;
353            }
354    
355            protected void validate(
356                            long layoutSetBranchId, long groupId, boolean privateLayout,
357                            String name, boolean master)
358                    throws PortalException, SystemException {
359    
360                    if (Validator.isNull(name) || (name.length() < 4)) {
361                            throw new LayoutSetBranchNameException(
362                                    LayoutSetBranchNameException.TOO_SHORT);
363                    }
364    
365                    if (name.length() > 100) {
366                            throw new LayoutSetBranchNameException(
367                                    LayoutSetBranchNameException.TOO_LONG);
368                    }
369    
370                    try {
371                            LayoutSetBranch layoutSetBranch =
372                                    layoutSetBranchPersistence.findByG_P_N(
373                                            groupId, privateLayout, name);
374    
375                            if (layoutSetBranch.getLayoutSetBranchId() != layoutSetBranchId) {
376                                    throw new LayoutSetBranchNameException(
377                                            LayoutSetBranchNameException.DUPLICATE);
378                            }
379                    }
380                    catch (NoSuchLayoutSetBranchException nsbe) {
381                    }
382    
383                    if (master) {
384                            try {
385                                    LayoutSetBranch masterLayoutSetBranch =
386                                            layoutSetBranchFinder.findByMaster(groupId, privateLayout);
387    
388                                    if (layoutSetBranchId !=
389                                                    masterLayoutSetBranch.getLayoutSetBranchId()) {
390    
391                                            throw new LayoutSetBranchNameException(
392                                                    LayoutSetBranchNameException.MASTER);
393                                    }
394                            }
395                            catch (NoSuchLayoutSetBranchException nsbe) {
396                            }
397                    }
398            }
399    
400    }