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.portlet.dynamicdatalists.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ResourceConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portlet.dynamicdatalists.RecordSetDDMStructureIdException;
027    import com.liferay.portlet.dynamicdatalists.RecordSetDuplicateRecordSetKeyException;
028    import com.liferay.portlet.dynamicdatalists.RecordSetNameException;
029    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
030    import com.liferay.portlet.dynamicdatalists.service.base.DDLRecordSetLocalServiceBaseImpl;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032    
033    import java.util.Date;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * Provides the local service for accessing, adding, deleting, and updating
040     * dynamic data list (DDL) record sets.
041     *
042     * @author Brian Wing Shun Chan
043     * @author Marcellus Tavares
044     */
045    public class DDLRecordSetLocalServiceImpl
046            extends DDLRecordSetLocalServiceBaseImpl {
047    
048            public DDLRecordSet addRecordSet(
049                            long userId, long groupId, long ddmStructureId, String recordSetKey,
050                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
051                            int minDisplayRows, int scope, ServiceContext serviceContext)
052                    throws PortalException, SystemException {
053    
054                    // Record set
055    
056                    User user = userPersistence.findByPrimaryKey(userId);
057    
058                    if (Validator.isNull(recordSetKey)) {
059                            recordSetKey = String.valueOf(counterLocalService.increment());
060                    }
061    
062                    Date now = new Date();
063    
064                    validate(groupId, ddmStructureId, recordSetKey, nameMap);
065    
066                    long recordSetId = counterLocalService.increment();
067    
068                    DDLRecordSet recordSet = ddlRecordSetPersistence.create(recordSetId);
069    
070                    recordSet.setUuid(serviceContext.getUuid());
071                    recordSet.setGroupId(groupId);
072                    recordSet.setCompanyId(user.getCompanyId());
073                    recordSet.setUserId(user.getUserId());
074                    recordSet.setUserName(user.getFullName());
075                    recordSet.setCreateDate(serviceContext.getCreateDate(now));
076                    recordSet.setModifiedDate(serviceContext.getModifiedDate(now));
077                    recordSet.setDDMStructureId(ddmStructureId);
078                    recordSet.setRecordSetKey(recordSetKey);
079                    recordSet.setNameMap(nameMap);
080                    recordSet.setDescriptionMap(descriptionMap);
081                    recordSet.setMinDisplayRows(minDisplayRows);
082                    recordSet.setScope(scope);
083    
084                    ddlRecordSetPersistence.update(recordSet);
085    
086                    // Resources
087    
088                    if (serviceContext.isAddGroupPermissions() ||
089                            serviceContext.isAddGuestPermissions()) {
090    
091                            addRecordSetResources(
092                                    recordSet, serviceContext.isAddGroupPermissions(),
093                                    serviceContext.isAddGuestPermissions());
094                    }
095                    else {
096                            addRecordSetResources(
097                                    recordSet, serviceContext.getGroupPermissions(),
098                                    serviceContext.getGuestPermissions());
099                    }
100    
101                    // Dynamic data mapping structure link
102    
103                    long classNameId = PortalUtil.getClassNameId(DDLRecordSet.class);
104    
105                    ddmStructureLinkLocalService.addStructureLink(
106                            classNameId, recordSetId, ddmStructureId, serviceContext);
107    
108                    return recordSet;
109            }
110    
111            public void addRecordSetResources(
112                            DDLRecordSet recordSet, boolean addGroupPermissions,
113                            boolean addGuestPermissions)
114                    throws PortalException, SystemException {
115    
116                    resourceLocalService.addResources(
117                            recordSet.getCompanyId(), recordSet.getGroupId(),
118                            recordSet.getUserId(), DDLRecordSet.class.getName(),
119                            recordSet.getRecordSetId(), false, addGroupPermissions,
120                            addGuestPermissions);
121            }
122    
123            public void addRecordSetResources(
124                            DDLRecordSet recordSet, String[] groupPermissions,
125                            String[] guestPermissions)
126                    throws PortalException, SystemException {
127    
128                    resourceLocalService.addModelResources(
129                            recordSet.getCompanyId(), recordSet.getGroupId(),
130                            recordSet.getUserId(), DDLRecordSet.class.getName(),
131                            recordSet.getRecordSetId(), groupPermissions, guestPermissions);
132            }
133    
134            public void deleteRecordSet(DDLRecordSet recordSet)
135                    throws PortalException, SystemException {
136    
137                    // Record set
138    
139                    ddlRecordSetPersistence.remove(recordSet);
140    
141                    // Resources
142    
143                    resourceLocalService.deleteResource(
144                            recordSet.getCompanyId(), DDLRecordSet.class.getName(),
145                            ResourceConstants.SCOPE_INDIVIDUAL, recordSet.getRecordSetId());
146    
147                    // Records
148    
149                    ddlRecordLocalService.deleteRecords(recordSet.getRecordSetId());
150    
151                    // Dynamic data mapping structure link
152    
153                    ddmStructureLinkLocalService.deleteClassStructureLink(
154                            recordSet.getRecordSetId());
155            }
156    
157            public void deleteRecordSet(long recordSetId)
158                    throws PortalException, SystemException {
159    
160                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
161                            recordSetId);
162    
163                    deleteRecordSet(recordSet);
164            }
165    
166            public void deleteRecordSet(long groupId, String recordSetKey)
167                    throws PortalException, SystemException {
168    
169                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByG_R(
170                            groupId, recordSetKey);
171    
172                    deleteRecordSet(recordSet);
173            }
174    
175            public void deleteRecordSets(long groupId)
176                    throws PortalException, SystemException {
177    
178                    List<DDLRecordSet> recordSets = ddlRecordSetPersistence.findByGroupId(
179                            groupId);
180    
181                    for (DDLRecordSet recordSet : recordSets) {
182                            deleteRecordSet(recordSet);
183                    }
184            }
185    
186            public DDLRecordSet fetchRecordSet(long groupId, String recordSetKey)
187                    throws SystemException {
188    
189                    return ddlRecordSetPersistence.fetchByG_R(groupId, recordSetKey);
190            }
191    
192            public DDLRecordSet getRecordSet(long recordSetId)
193                    throws PortalException, SystemException {
194    
195                    return ddlRecordSetPersistence.findByPrimaryKey(recordSetId);
196            }
197    
198            public DDLRecordSet getRecordSet(long groupId, String recordSetKey)
199                    throws PortalException, SystemException {
200    
201                    return ddlRecordSetPersistence.findByG_R(groupId, recordSetKey);
202            }
203    
204            public List<DDLRecordSet> getRecordSets(long groupId)
205                    throws SystemException {
206    
207                    return ddlRecordSetPersistence.findByGroupId(groupId);
208            }
209    
210            public int getRecordSetsCount(long groupId) throws SystemException {
211                    return ddlRecordSetPersistence.countByGroupId(groupId);
212            }
213    
214            public List<DDLRecordSet> search(
215                            long companyId, long groupId, String keywords, int scope, int start,
216                            int end, OrderByComparator orderByComparator)
217                    throws SystemException {
218    
219                    return ddlRecordSetFinder.findByKeywords(
220                            companyId, groupId, keywords, scope, start, end, orderByComparator);
221            }
222    
223            public List<DDLRecordSet> search(
224                            long companyId, long groupId, String name, String description,
225                            int scope, boolean andOperator, int start, int end,
226                            OrderByComparator orderByComparator)
227                    throws SystemException {
228    
229                    return ddlRecordSetFinder.findByC_G_N_D_S(
230                            companyId, groupId, name, description, scope, andOperator, start,
231                            end, orderByComparator);
232            }
233    
234            public int searchCount(
235                            long companyId, long groupId, String keywords, int scope)
236                    throws SystemException {
237    
238                    return ddlRecordSetFinder.countByKeywords(
239                            companyId, groupId, keywords, scope);
240            }
241    
242            public int searchCount(
243                            long companyId, long groupId, String name, String description,
244                            int scope, boolean andOperator)
245                    throws SystemException {
246    
247                    return ddlRecordSetFinder.countByC_G_N_D_S(
248                            companyId, groupId, name, description, scope, andOperator);
249            }
250    
251            public DDLRecordSet updateMinDisplayRows(
252                            long recordSetId, int minDisplayRows, ServiceContext serviceContext)
253                    throws PortalException, SystemException {
254    
255                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
256                            recordSetId);
257    
258                    recordSet.setModifiedDate(serviceContext.getModifiedDate(null));
259                    recordSet.setMinDisplayRows(minDisplayRows);
260    
261                    ddlRecordSetPersistence.update(recordSet);
262    
263                    return recordSet;
264            }
265    
266            public DDLRecordSet updateRecordSet(
267                            long recordSetId, long ddmStructureId, Map<Locale, String> nameMap,
268                            Map<Locale, String> descriptionMap, int minDisplayRows,
269                            ServiceContext serviceContext)
270                    throws PortalException, SystemException {
271    
272                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
273                            recordSetId);
274    
275                    return doUpdateRecordSet(
276                            ddmStructureId, nameMap, descriptionMap, minDisplayRows,
277                            serviceContext, recordSet);
278            }
279    
280            public DDLRecordSet updateRecordSet(
281                            long groupId, long ddmStructureId, String recordSetKey,
282                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
283                            int minDisplayRows, ServiceContext serviceContext)
284                    throws PortalException, SystemException {
285    
286                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByG_R(
287                            groupId, recordSetKey);
288    
289                    return doUpdateRecordSet(
290                            ddmStructureId, nameMap, descriptionMap, minDisplayRows,
291                            serviceContext, recordSet);
292            }
293    
294            protected DDLRecordSet doUpdateRecordSet(
295                            long ddmStructureId, Map<Locale, String> nameMap,
296                            Map<Locale, String> descriptionMap, int minDisplayRows,
297                            ServiceContext serviceContext, DDLRecordSet recordSet)
298                    throws PortalException, SystemException {
299    
300                    validateDDMStructureId(ddmStructureId);
301                    validateName(nameMap);
302    
303                    recordSet.setModifiedDate(serviceContext.getModifiedDate(null));
304                    recordSet.setDDMStructureId(ddmStructureId);
305                    recordSet.setNameMap(nameMap);
306                    recordSet.setDescriptionMap(descriptionMap);
307                    recordSet.setMinDisplayRows(minDisplayRows);
308    
309                    ddlRecordSetPersistence.update(recordSet);
310    
311                    return recordSet;
312            }
313    
314            protected void validate(
315                            long groupId, long ddmStructureId, String recordSetKey,
316                            Map<Locale, String> nameMap)
317                    throws PortalException, SystemException {
318    
319                    validateDDMStructureId(ddmStructureId);
320    
321                    if (Validator.isNotNull(recordSetKey)) {
322                            DDLRecordSet recordSet = ddlRecordSetPersistence.fetchByG_R(
323                                    groupId, recordSetKey);
324    
325                            if (recordSet != null) {
326                                    RecordSetDuplicateRecordSetKeyException rsdrske =
327                                            new RecordSetDuplicateRecordSetKeyException();
328    
329                                    rsdrske.setRecordSetKey(recordSet.getRecordSetKey());
330    
331                                    throw rsdrske;
332                            }
333                    }
334    
335                    validateName(nameMap);
336            }
337    
338            protected void validateDDMStructureId(long ddmStructureId)
339                    throws PortalException, SystemException {
340    
341                    DDMStructure ddmStructure = ddmStructurePersistence.fetchByPrimaryKey(
342                            ddmStructureId);
343    
344                    if (ddmStructure == null) {
345                            throw new RecordSetDDMStructureIdException();
346                    }
347            }
348    
349            protected void validateName(Map<Locale, String> nameMap)
350                    throws PortalException {
351    
352                    String name = nameMap.get(LocaleUtil.getDefault());
353    
354                    if (Validator.isNull(name)) {
355                            throw new RecordSetNameException();
356                    }
357            }
358    
359    }