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