001    /**
002     * Copyright (c) 2000-2013 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.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                    User user = userPersistence.findByPrimaryKey(
048                            serviceContext.getUserId());
049                    LayoutSetBranch layoutSetBranch =
050                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
051    
052                    validate(0, layoutSetBranchId, plid, name);
053    
054                    long layoutBranchId = counterLocalService.increment();
055    
056                    LayoutBranch layoutBranch = layoutBranchPersistence.create(
057                            layoutBranchId);
058    
059                    layoutBranch.setGroupId(layoutSetBranch.getGroupId());
060                    layoutBranch.setCompanyId(user.getCompanyId());
061                    layoutBranch.setUserId(user.getUserId());
062                    layoutBranch.setUserName(user.getFullName());
063                    layoutBranch.setLayoutSetBranchId(layoutSetBranchId);
064                    layoutBranch.setPlid(plid);
065                    layoutBranch.setName(name);
066                    layoutBranch.setDescription(description);
067                    layoutBranch.setMaster(master);
068    
069                    layoutBranchPersistence.update(layoutBranch);
070    
071                    StagingUtil.setRecentLayoutBranchId(
072                            user, layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(),
073                            layoutBranch.getLayoutBranchId());
074    
075                    return layoutBranch;
076            }
077    
078            @Override
079            public LayoutBranch addLayoutBranch(
080                            long layoutRevisionId, String name, String description,
081                            boolean master, ServiceContext serviceContext)
082                    throws PortalException, SystemException {
083    
084                    LayoutRevision layoutRevision =
085                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
086    
087                    LayoutBranch layoutBranch = addLayoutBranch(
088                            layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(),
089                            name, description, master, serviceContext);
090    
091                    layoutRevisionService.addLayoutRevision(
092                            layoutBranch.getUserId(), layoutRevision.getLayoutSetBranchId(),
093                            layoutBranch.getLayoutBranchId(),
094                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID, false,
095                            layoutRevision.getPlid(), layoutRevision.getLayoutRevisionId(),
096                            layoutRevision.isPrivateLayout(), layoutRevision.getName(),
097                            layoutRevision.getTitle(), layoutRevision.getDescription(),
098                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
099                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
100                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
101                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
102                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
103                            serviceContext);
104    
105                    return layoutBranch;
106            }
107    
108            @Override
109            public LayoutBranch deleteLayoutBranch(long layoutBranchId)
110                    throws PortalException, SystemException {
111    
112                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
113                            layoutBranchId);
114    
115                    layoutRevisionLocalService.deleteLayoutRevisions(
116                            layoutBranch.getLayoutSetBranchId(), layoutBranchId,
117                            layoutBranch.getPlid());
118    
119                    return layoutBranchLocalService.deleteLayoutBranch(layoutBranch);
120            }
121    
122            @Override
123            public void deleteLayoutSetBranchLayoutBranches(long layoutSetBranchId)
124                    throws PortalException, SystemException {
125    
126                    List<LayoutBranch> layoutBranches =
127                            layoutBranchPersistence.findByLayoutSetBranchId(layoutSetBranchId);
128    
129                    for (LayoutBranch layoutBranch : layoutBranches) {
130                            deleteLayoutBranch(layoutBranch.getLayoutBranchId());
131                    }
132            }
133    
134            @Override
135            public List<LayoutBranch> getLayoutBranches(
136                            long layoutSetBranchId, long plid, int start, int end,
137                            OrderByComparator orderByComparator)
138                    throws SystemException {
139    
140                    return layoutBranchPersistence.findByL_P(
141                            layoutSetBranchId, plid, start, end, orderByComparator);
142            }
143    
144            @Override
145            public List<LayoutBranch> getLayoutSetBranchLayoutBranches(
146                            long layoutSetBranchId)
147                    throws SystemException {
148    
149                    return layoutBranchPersistence.findByLayoutSetBranchId(
150                            layoutSetBranchId);
151            }
152    
153            @Override
154            public LayoutBranch getMasterLayoutBranch(long layoutSetBranchId, long plid)
155                    throws PortalException, SystemException {
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, SystemException {
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, SystemException {
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, SystemException {
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    }