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.LayoutBranchNameException;
018    import com.liferay.portal.exception.NoSuchLayoutBranchException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.LayoutBranch;
023    import com.liferay.portal.model.LayoutBranchConstants;
024    import com.liferay.portal.model.LayoutRevision;
025    import com.liferay.portal.model.LayoutRevisionConstants;
026    import com.liferay.portal.model.LayoutSetBranch;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.base.LayoutBranchLocalServiceBaseImpl;
030    import com.liferay.portlet.exportimport.staging.StagingUtil;
031    
032    import java.util.List;
033    
034    /**
035     * @author Julio Camarero
036     */
037    public class LayoutBranchLocalServiceImpl
038            extends LayoutBranchLocalServiceBaseImpl {
039    
040            @Override
041            public LayoutBranch addLayoutBranch(
042                            long layoutSetBranchId, long plid, String name, String description,
043                            boolean master, ServiceContext serviceContext)
044                    throws PortalException {
045    
046                    User user = userPersistence.findByPrimaryKey(
047                            serviceContext.getUserId());
048                    LayoutSetBranch layoutSetBranch =
049                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
050    
051                    validate(0, layoutSetBranchId, plid, name);
052    
053                    long layoutBranchId = counterLocalService.increment();
054    
055                    LayoutBranch layoutBranch = layoutBranchPersistence.create(
056                            layoutBranchId);
057    
058                    layoutBranch.setGroupId(layoutSetBranch.getGroupId());
059                    layoutBranch.setCompanyId(user.getCompanyId());
060                    layoutBranch.setUserId(user.getUserId());
061                    layoutBranch.setUserName(user.getFullName());
062                    layoutBranch.setLayoutSetBranchId(layoutSetBranchId);
063                    layoutBranch.setPlid(plid);
064                    layoutBranch.setName(name);
065                    layoutBranch.setDescription(description);
066                    layoutBranch.setMaster(master);
067    
068                    layoutBranchPersistence.update(layoutBranch);
069    
070                    StagingUtil.setRecentLayoutBranchId(
071                            user, layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(),
072                            layoutBranch.getLayoutBranchId());
073    
074                    return layoutBranch;
075            }
076    
077            @Override
078            public LayoutBranch addLayoutBranch(
079                            long layoutRevisionId, String name, String description,
080                            boolean master, ServiceContext serviceContext)
081                    throws PortalException {
082    
083                    LayoutRevision layoutRevision =
084                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
085    
086                    LayoutBranch layoutBranch = addLayoutBranch(
087                            layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(),
088                            name, description, master, serviceContext);
089    
090                    layoutRevisionLocalService.addLayoutRevision(
091                            layoutBranch.getUserId(), layoutRevision.getLayoutSetBranchId(),
092                            layoutBranch.getLayoutBranchId(),
093                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID, false,
094                            layoutRevision.getPlid(), layoutRevision.getLayoutRevisionId(),
095                            layoutRevision.isPrivateLayout(), layoutRevision.getName(),
096                            layoutRevision.getTitle(), layoutRevision.getDescription(),
097                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
098                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
099                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
100                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
101                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
102                            serviceContext);
103    
104                    return layoutBranch;
105            }
106    
107            @Override
108            public LayoutBranch deleteLayoutBranch(long layoutBranchId)
109                    throws PortalException {
110    
111                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
112                            layoutBranchId);
113    
114                    layoutRevisionLocalService.deleteLayoutRevisions(
115                            layoutBranch.getLayoutSetBranchId(), layoutBranchId,
116                            layoutBranch.getPlid());
117    
118                    recentLayoutBranchLocalService.deleteRecentLayoutBranches(
119                            layoutBranch.getLayoutBranchId());
120    
121                    return deleteLayoutBranch(layoutBranch);
122            }
123    
124            @Override
125            public void deleteLayoutSetBranchLayoutBranches(long layoutSetBranchId)
126                    throws PortalException {
127    
128                    List<LayoutBranch> layoutBranches =
129                            layoutBranchPersistence.findByLayoutSetBranchId(layoutSetBranchId);
130    
131                    for (LayoutBranch layoutBranch : layoutBranches) {
132                            deleteLayoutBranch(layoutBranch.getLayoutBranchId());
133                    }
134            }
135    
136            @Override
137            public List<LayoutBranch> getLayoutBranches(
138                    long layoutSetBranchId, long plid, int start, int end,
139                    OrderByComparator<LayoutBranch> orderByComparator) {
140    
141                    return layoutBranchPersistence.findByL_P(
142                            layoutSetBranchId, plid, start, end, orderByComparator);
143            }
144    
145            @Override
146            public List<LayoutBranch> getLayoutSetBranchLayoutBranches(
147                    long layoutSetBranchId) {
148    
149                    return layoutBranchPersistence.findByLayoutSetBranchId(
150                            layoutSetBranchId);
151            }
152    
153            @Override
154            public LayoutBranch getMasterLayoutBranch(long layoutSetBranchId, long plid)
155                    throws PortalException {
156    
157                    return layoutBranchPersistence.findByL_P_M_First(
158                            layoutSetBranchId, plid, true, null);
159            }
160    
161            @Override
162            public LayoutBranch getMasterLayoutBranch(
163                            long layoutSetBranchId, long plid, ServiceContext serviceContext)
164                    throws PortalException {
165    
166                    LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_M_First(
167                            layoutSetBranchId, plid, true, null);
168    
169                    if (layoutBranch != null) {
170                            return layoutBranch;
171                    }
172    
173                    return layoutBranchLocalService.addLayoutBranch(
174                            layoutSetBranchId, plid, LayoutBranchConstants.MASTER_BRANCH_NAME,
175                            LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
176                            serviceContext);
177            }
178    
179            @Override
180            public LayoutBranch updateLayoutBranch(
181                            long layoutBranchId, String name, String description,
182                            ServiceContext serviceContext)
183                    throws PortalException {
184    
185                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
186                            layoutBranchId);
187    
188                    validate(
189                            layoutBranch.getLayoutBranchId(),
190                            layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(), name);
191    
192                    layoutBranch.setName(name);
193                    layoutBranch.setDescription(description);
194    
195                    layoutBranchPersistence.update(layoutBranch);
196    
197                    return layoutBranch;
198            }
199    
200            protected void validate(
201                            long layoutBranchId, long layoutSetBranchId, long plid, String name)
202                    throws PortalException {
203    
204                    if (Validator.isNull(name) || (name.length() < 4)) {
205                            throw new LayoutBranchNameException(
206                                    LayoutBranchNameException.TOO_SHORT);
207                    }
208    
209                    if (name.length() > 100) {
210                            throw new LayoutBranchNameException(
211                                    LayoutBranchNameException.TOO_LONG);
212                    }
213    
214                    try {
215                            LayoutBranch layoutBranch = layoutBranchPersistence.findByL_P_N(
216                                    layoutSetBranchId, plid, name);
217    
218                            if (layoutBranch.getLayoutBranchId() != layoutBranchId) {
219                                    throw new LayoutBranchNameException(
220                                            LayoutBranchNameException.DUPLICATE);
221                            }
222                    }
223                    catch (NoSuchLayoutBranchException nslbe) {
224                    }
225            }
226    
227    }