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