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