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