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