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