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.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.dynamicdatamapping.StorageException;
023    import com.liferay.portlet.dynamicdatamapping.StorageFieldNameException;
024    import com.liferay.portlet.dynamicdatamapping.StorageFieldRequiredException;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMStorageLink;
026    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
027    import com.liferay.portlet.dynamicdatamapping.service.DDMStorageLinkLocalServiceUtil;
028    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
029    import com.liferay.portlet.dynamicdatamapping.storage.query.Condition;
030    
031    import java.util.Iterator;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * @author Eduardo Lundgren
037     * @author Brian Wing Shun Chan
038     * @author Marcellus Tavares
039     */
040    public abstract class BaseStorageAdapter implements StorageAdapter {
041    
042            @Override
043            public long create(
044                            long companyId, long ddmStructureId, Fields fields,
045                            ServiceContext serviceContext)
046                    throws StorageException {
047    
048                    try {
049                            validateDDMStructureFields(ddmStructureId, fields);
050    
051                            return doCreate(companyId, ddmStructureId, fields, serviceContext);
052                    }
053                    catch (StorageException se) {
054                            throw se;
055                    }
056                    catch (Exception e) {
057                            throw new StorageException(e);
058                    }
059            }
060    
061            @Override
062            public void deleteByClass(long classPK) throws StorageException {
063                    try {
064                            doDeleteByClass(classPK);
065                    }
066                    catch (StorageException se) {
067                            throw se;
068                    }
069                    catch (Exception e) {
070                            throw new StorageException(e);
071                    }
072            }
073    
074            @Override
075            public void deleteByDDMStructure(long ddmStructureId)
076                    throws StorageException {
077    
078                    try {
079                            doDeleteByDDMStructure(ddmStructureId);
080                    }
081                    catch (StorageException se) {
082                            throw se;
083                    }
084                    catch (Exception e) {
085                            throw new StorageException(e);
086                    }
087            }
088    
089            @Override
090            public Fields getFields(long classPK) throws StorageException {
091                    return getFields(classPK, null);
092            }
093    
094            @Override
095            public Fields getFields(long classPK, List<String> fieldNames)
096                    throws StorageException {
097    
098                    try {
099                            DDMStorageLink ddmStorageLink =
100                                    DDMStorageLinkLocalServiceUtil.getClassStorageLink(classPK);
101    
102                            Map<Long, Fields> fieldsMapByClasses = getFieldsMap(
103                                    ddmStorageLink.getStructureId(), new long[] {classPK},
104                                    fieldNames);
105    
106                            return fieldsMapByClasses.get(classPK);
107                    }
108                    catch (StorageException se) {
109                            throw se;
110                    }
111                    catch (Exception e) {
112                            throw new StorageException(e);
113                    }
114            }
115    
116            @Override
117            public List<Fields> getFieldsList(
118                            long ddmStructureId, List<String> fieldNames)
119                    throws StorageException {
120    
121                    return getFieldsList(ddmStructureId, fieldNames, null);
122            }
123    
124            @Override
125            public List<Fields> getFieldsList(
126                            long ddmStructureId, List<String> fieldNames,
127                            OrderByComparator orderByComparator)
128                    throws StorageException {
129    
130                    try {
131                            return doGetFieldsListByDDMStructure(
132                                    ddmStructureId, fieldNames, orderByComparator);
133                    }
134                    catch (StorageException se) {
135                            throw se;
136                    }
137                    catch (Exception e) {
138                            throw new StorageException(e);
139                    }
140            }
141    
142            @Override
143            public List<Fields> getFieldsList(
144                            long ddmStructureId, long[] classPKs, List<String> fieldNames,
145                            OrderByComparator orderByComparator)
146                    throws StorageException {
147    
148                    try {
149                            return doGetFieldsListByClasses(
150                                    ddmStructureId, classPKs, fieldNames, orderByComparator);
151                    }
152                    catch (StorageException se) {
153                            throw se;
154                    }
155                    catch (Exception e) {
156                            throw new StorageException(e);
157                    }
158            }
159    
160            @Override
161            public List<Fields> getFieldsList(
162                            long ddmStructureId, long[] classPKs,
163                            OrderByComparator orderByComparator)
164                    throws StorageException {
165    
166                    return getFieldsList(ddmStructureId, classPKs, null, orderByComparator);
167            }
168    
169            @Override
170            public Map<Long, Fields> getFieldsMap(long ddmStructureId, long[] classPKs)
171                    throws StorageException {
172    
173                    return getFieldsMap(ddmStructureId, classPKs, null);
174            }
175    
176            @Override
177            public Map<Long, Fields> getFieldsMap(
178                            long ddmStructureId, long[] classPKs, List<String> fieldNames)
179                    throws StorageException {
180    
181                    try {
182                            return doGetFieldsMapByClasses(
183                                    ddmStructureId, classPKs, fieldNames);
184                    }
185                    catch (StorageException se) {
186                            throw se;
187                    }
188                    catch (Exception e) {
189                            throw new StorageException(e);
190                    }
191            }
192    
193            @Override
194            public List<Fields> query(
195                            long ddmStructureId, List<String> fieldNames, Condition condition,
196                            OrderByComparator orderByComparator)
197                    throws StorageException {
198    
199                    try {
200                            return doQuery(
201                                    ddmStructureId, fieldNames, condition, orderByComparator);
202                    }
203                    catch (StorageException se) {
204                            throw se;
205                    }
206                    catch (Exception e) {
207                            throw new StorageException(e);
208                    }
209            }
210    
211            @Override
212            public int queryCount(long ddmStructureId, Condition condition)
213                    throws StorageException {
214    
215                    try {
216                            return doQueryCount(ddmStructureId, condition);
217                    }
218                    catch (StorageException se) {
219                            throw se;
220                    }
221                    catch (Exception e) {
222                            throw new StorageException(e);
223                    }
224            }
225    
226            @Override
227            public void update(
228                            long classPK, Fields fields, boolean mergeFields,
229                            ServiceContext serviceContext)
230                    throws StorageException {
231    
232                    try {
233                            validateClassFields(classPK, fields);
234    
235                            doUpdate(classPK, fields, mergeFields, serviceContext);
236                    }
237                    catch (StorageException se) {
238                            throw se;
239                    }
240                    catch (Exception e) {
241                            throw new StorageException(e);
242                    }
243            }
244    
245            @Override
246            public void update(
247                            long classPK, Fields fields, ServiceContext serviceContext)
248                    throws StorageException {
249    
250                    update(classPK, fields, false, serviceContext);
251            }
252    
253            protected abstract long doCreate(
254                            long companyId, long ddmStructureId, Fields fields,
255                            ServiceContext serviceContext)
256                    throws Exception;
257    
258            protected abstract void doDeleteByClass(long classPK) throws Exception;
259    
260            protected abstract void doDeleteByDDMStructure(long ddmStructureId)
261                    throws Exception;
262    
263            protected abstract List<Fields> doGetFieldsListByClasses(
264                            long ddmStructureId, long[] classPKs, List<String> fieldNames,
265                            OrderByComparator orderByComparator)
266                    throws Exception;
267    
268            protected abstract List<Fields> doGetFieldsListByDDMStructure(
269                            long ddmStructureId, List<String> fieldNames,
270                            OrderByComparator orderByComparator)
271                    throws Exception;
272    
273            protected abstract Map<Long, Fields> doGetFieldsMapByClasses(
274                            long ddmStructureId, long[] classPKs, List<String> fieldNames)
275                    throws Exception;
276    
277            protected abstract List<Fields> doQuery(
278                            long ddmStructureId, List<String> fieldNames, Condition condition,
279                            OrderByComparator orderByComparator)
280                    throws Exception;
281    
282            protected abstract int doQueryCount(
283                            long ddmStructureId, Condition condition)
284                    throws Exception;
285    
286            protected abstract void doUpdate(
287                            long classPK, Fields fields, boolean mergeFields,
288                            ServiceContext serviceContext)
289                    throws Exception;
290    
291            protected void validateClassFields(long classPK, Fields fields)
292                    throws PortalException, SystemException {
293    
294                    DDMStorageLink ddmStorageLink =
295                            DDMStorageLinkLocalServiceUtil.getClassStorageLink(classPK);
296    
297                    validateDDMStructureFields(ddmStorageLink.getStructureId(), fields);
298            }
299    
300            protected void validateDDMStructureFields(
301                            long ddmStructureId, Fields fields)
302                    throws PortalException, SystemException {
303    
304                    DDMStructure ddmStructure =
305                            DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
306    
307                    Iterator<Field> itr = fields.iterator();
308    
309                    while (itr.hasNext()) {
310                            Field field = itr.next();
311    
312                            if (!ddmStructure.hasField(field.getName())) {
313                                    throw new StorageFieldNameException();
314                            }
315    
316                            if (ddmStructure.getFieldRequired(field.getName()) &&
317                                    Validator.isNull(field.getValue())) {
318    
319                                    throw new StorageFieldRequiredException();
320                            }
321                    }
322            }
323    
324    }