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.NoSuchLayoutFriendlyURLException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.LayoutFriendlyURL;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.base.LayoutFriendlyURLLocalServiceBaseImpl;
027    
028    import java.util.ArrayList;
029    import java.util.Date;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    /**
035     * Provides the local service for accessing, adding, deleting, and updating
036     * friendly URLs for layouts.
037     *
038     * <p>
039     * All custom service methods should be put in this class. Whenever methods are
040     * added, rerun ServiceBuilder to copy their definitions into the {@link
041     * com.liferay.portal.service.LayoutFriendlyURLLocalService} interface.
042     * </p>
043     *
044     * <p>
045     * Methods of this service will not have security checks based on the propagated
046     * JAAS credentials because this service can only be accessed from within the
047     * same VM.
048     * </p>
049     *
050     * @author Brian Wing Shun Chan
051     */
052    public class LayoutFriendlyURLLocalServiceImpl
053            extends LayoutFriendlyURLLocalServiceBaseImpl {
054    
055            @Override
056            public LayoutFriendlyURL addLayoutFriendlyURL(
057                            long userId, long companyId, long groupId, long plid,
058                            boolean privateLayout, String friendlyURL, String languageId,
059                            ServiceContext serviceContext)
060                    throws PortalException {
061    
062                    User user = userPersistence.findByPrimaryKey(userId);
063                    Date now = new Date();
064    
065                    long layoutFriendlyURLId = counterLocalService.increment();
066    
067                    LayoutFriendlyURL layoutFriendlyURL =
068                            layoutFriendlyURLPersistence.create(layoutFriendlyURLId);
069    
070                    layoutFriendlyURL.setUuid(serviceContext.getUuid());
071                    layoutFriendlyURL.setGroupId(groupId);
072                    layoutFriendlyURL.setCompanyId(companyId);
073                    layoutFriendlyURL.setUserId(user.getUserId());
074                    layoutFriendlyURL.setUserName(user.getFullName());
075                    layoutFriendlyURL.setCreateDate(serviceContext.getCreateDate(now));
076                    layoutFriendlyURL.setModifiedDate(serviceContext.getModifiedDate(now));
077                    layoutFriendlyURL.setPlid(plid);
078                    layoutFriendlyURL.setPrivateLayout(privateLayout);
079                    layoutFriendlyURL.setFriendlyURL(friendlyURL);
080                    layoutFriendlyURL.setLanguageId(languageId);
081    
082                    return layoutFriendlyURLPersistence.update(layoutFriendlyURL);
083            }
084    
085            @Override
086            public List<LayoutFriendlyURL> addLayoutFriendlyURLs(
087                            long userId, long companyId, long groupId, long plid,
088                            boolean privateLayout, Map<Locale, String> friendlyURLMap,
089                            ServiceContext serviceContext)
090                    throws PortalException {
091    
092                    List<LayoutFriendlyURL> layoutFriendlyURLs =
093                            new ArrayList<LayoutFriendlyURL>();
094    
095                    Locale[] locales = LanguageUtil.getAvailableLocales(groupId);
096    
097                    for (Locale locale : locales) {
098                            String friendlyURL = friendlyURLMap.get(locale);
099    
100                            if (Validator.isNull(friendlyURL)) {
101                                    continue;
102                            }
103    
104                            LayoutFriendlyURL layoutFriendlyURL = addLayoutFriendlyURL(
105                                    userId, companyId, groupId, plid, privateLayout, friendlyURL,
106                                    LocaleUtil.toLanguageId(locale), serviceContext);
107    
108                            layoutFriendlyURLs.add(layoutFriendlyURL);
109                    }
110    
111                    return layoutFriendlyURLs;
112            }
113    
114            @Override
115            public LayoutFriendlyURL deleteLayoutFriendlyURL(
116                    LayoutFriendlyURL layoutFriendlyURL) {
117    
118                    return layoutFriendlyURLPersistence.remove(layoutFriendlyURL);
119            }
120    
121            @Override
122            public void deleteLayoutFriendlyURL(long plid, String languageId) {
123                    LayoutFriendlyURL layoutFriendlyURL =
124                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
125    
126                    if (layoutFriendlyURL != null) {
127                            deleteLayoutFriendlyURL(layoutFriendlyURL);
128                    }
129            }
130    
131            @Override
132            public void deleteLayoutFriendlyURLs(long plid) {
133                    List<LayoutFriendlyURL> layoutFriendlyURLs =
134                            layoutFriendlyURLPersistence.findByPlid(plid);
135    
136                    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
137                            deleteLayoutFriendlyURL(layoutFriendlyURL);
138                    }
139            }
140    
141            @Override
142            public LayoutFriendlyURL fetchFirstLayoutFriendlyURL(
143                    long groupId, boolean privateLayout, String friendlyURL) {
144    
145                    return layoutFriendlyURLPersistence.fetchByG_P_F_First(
146                            groupId, privateLayout, friendlyURL, null);
147            }
148    
149            @Override
150            public LayoutFriendlyURL fetchLayoutFriendlyURL(
151                    long groupId, boolean privateLayout, String friendlyURL,
152                    String languageId) {
153    
154                    return layoutFriendlyURLPersistence.fetchByG_P_F_L(
155                            groupId, privateLayout, friendlyURL, languageId);
156            }
157    
158            @Override
159            public LayoutFriendlyURL fetchLayoutFriendlyURL(
160                    long plid, String languageId) {
161    
162                    return fetchLayoutFriendlyURL(plid, languageId, true);
163            }
164    
165            @Override
166            public LayoutFriendlyURL fetchLayoutFriendlyURL(
167                    long plid, String languageId, boolean useDefault) {
168    
169                    LayoutFriendlyURL layoutFriendlyURL =
170                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
171    
172                    if ((layoutFriendlyURL == null) && !useDefault) {
173                            return null;
174                    }
175    
176                    if (layoutFriendlyURL == null) {
177                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByP_L(
178                                    plid, LocaleUtil.toLanguageId(LocaleUtil.getSiteDefault()));
179                    }
180    
181                    if (layoutFriendlyURL == null) {
182                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByPlid_First(
183                                    plid, null);
184                    }
185    
186                    return layoutFriendlyURL;
187            }
188    
189            @Override
190            public LayoutFriendlyURL getLayoutFriendlyURL(long plid, String languageId)
191                    throws PortalException {
192    
193                    return getLayoutFriendlyURL(plid, languageId, true);
194            }
195    
196            @Override
197            public LayoutFriendlyURL getLayoutFriendlyURL(
198                            long plid, String languageId, boolean useDefault)
199                    throws PortalException {
200    
201                    LayoutFriendlyURL layoutFriendlyURL =
202                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
203    
204                    if ((layoutFriendlyURL == null) && !useDefault) {
205                            StringBundler sb = new StringBundler(5);
206    
207                            sb.append("{plid=");
208                            sb.append(plid);
209                            sb.append(", languageId=");
210                            sb.append(languageId);
211                            sb.append("}");
212    
213                            throw new NoSuchLayoutFriendlyURLException(sb.toString());
214                    }
215    
216                    if (layoutFriendlyURL == null) {
217                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByP_L(
218                                    plid, LocaleUtil.toLanguageId(LocaleUtil.getSiteDefault()));
219                    }
220    
221                    if (layoutFriendlyURL == null) {
222                            layoutFriendlyURL = layoutFriendlyURLPersistence.findByPlid_First(
223                                    plid, null);
224                    }
225    
226                    return layoutFriendlyURL;
227            }
228    
229            @Override
230            public List<LayoutFriendlyURL> getLayoutFriendlyURLs(long plid) {
231                    return layoutFriendlyURLPersistence.findByPlid(plid);
232            }
233    
234            @Override
235            public List<LayoutFriendlyURL> getLayoutFriendlyURLs(
236                    long plid, String friendlyURL, int start, int end) {
237    
238                    return layoutFriendlyURLPersistence.findByP_F(
239                            plid, friendlyURL, start, end);
240            }
241    
242            @Override
243            public LayoutFriendlyURL updateLayoutFriendlyURL(
244                            long userId, long companyId, long groupId, long plid,
245                            boolean privateLayout, String friendlyURL, String languageId,
246                            ServiceContext serviceContext)
247                    throws PortalException {
248    
249                    LayoutFriendlyURL layoutFriendlyURL =
250                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
251    
252                    if (layoutFriendlyURL == null) {
253                            return addLayoutFriendlyURL(
254                                    userId, companyId, groupId, plid, privateLayout, friendlyURL,
255                                    languageId, serviceContext);
256                    }
257    
258                    layoutFriendlyURL.setFriendlyURL(friendlyURL);
259    
260                    return layoutFriendlyURLPersistence.update(layoutFriendlyURL);
261            }
262    
263            @Override
264            public List<LayoutFriendlyURL> updateLayoutFriendlyURLs(
265                            long userId, long companyId, long groupId, long plid,
266                            boolean privateLayout, Map<Locale, String> friendlyURLMap,
267                            ServiceContext serviceContext)
268                    throws PortalException {
269    
270                    List<LayoutFriendlyURL> layoutFriendlyURLs =
271                            new ArrayList<LayoutFriendlyURL>();
272    
273                    Locale[] locales = LanguageUtil.getAvailableLocales(groupId);
274    
275                    for (Locale locale : locales) {
276                            String friendlyURL = friendlyURLMap.get(locale);
277                            String languageId = LocaleUtil.toLanguageId(locale);
278    
279                            if (Validator.isNull(friendlyURL)) {
280                                    deleteLayoutFriendlyURL(plid, languageId);
281                            }
282                            else {
283                                    LayoutFriendlyURL layoutFriendlyURL = updateLayoutFriendlyURL(
284                                            userId, companyId, groupId, plid, privateLayout,
285                                            friendlyURL, languageId, serviceContext);
286    
287                                    layoutFriendlyURLs.add(layoutFriendlyURL);
288                            }
289                    }
290    
291                    return layoutFriendlyURLs;
292            }
293    
294    }