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