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