001    /**
002     * Copyright (c) 2000-present 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.util.OrderByComparator;
018    import com.liferay.portal.service.ServiceContext;
019    import com.liferay.portlet.dynamicdatamapping.StorageException;
020    import com.liferay.portlet.dynamicdatamapping.model.DDMStorageLink;
021    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
022    import com.liferay.portlet.dynamicdatamapping.service.DDMStorageLinkLocalServiceUtil;
023    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
024    import com.liferay.portlet.dynamicdatamapping.storage.query.Condition;
025    
026    import java.util.List;
027    import java.util.Map;
028    
029    /**
030     * @author Eduardo Lundgren
031     */
032    public class StorageEngineImpl implements StorageEngine {
033    
034            @Override
035            public long create(
036                            long companyId, long ddmStructureId, DDMFormValues ddmFormValues,
037                            ServiceContext serviceContext)
038                    throws StorageException {
039    
040                    StorageAdapter storageAdapter = getStructureStorageAdapter(
041                            ddmStructureId);
042    
043                    return storageAdapter.create(
044                            companyId, ddmStructureId, ddmFormValues, serviceContext);
045            }
046    
047            @Override
048            public long create(
049                            long companyId, long ddmStructureId, Fields fields,
050                            ServiceContext serviceContext)
051                    throws StorageException {
052    
053                    StorageAdapter storageAdapter = getStructureStorageAdapter(
054                            ddmStructureId);
055    
056                    return storageAdapter.create(
057                            companyId, ddmStructureId, fields, serviceContext);
058            }
059    
060            @Override
061            public void deleteByClass(long classPK) throws StorageException {
062                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
063    
064                    storageAdapter.deleteByClass(classPK);
065            }
066    
067            @Override
068            public void deleteByDDMStructure(long ddmStructureId)
069                    throws StorageException {
070    
071                    StorageAdapter storageAdapter = getStructureStorageAdapter(
072                            ddmStructureId);
073    
074                    storageAdapter.deleteByDDMStructure(ddmStructureId);
075            }
076    
077            @Override
078            public DDMFormValues getDDMFormValues(long classPK)
079                    throws StorageException {
080    
081                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
082    
083                    return storageAdapter.getDDMFormValues(classPK);
084            }
085    
086            @Override
087            public Fields getFields(long classPK) throws StorageException {
088                    return getFields(classPK, null);
089            }
090    
091            @Override
092            public Fields getFields(long classPK, List<String> fieldNames)
093                    throws StorageException {
094    
095                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
096    
097                    return storageAdapter.getFields(classPK, fieldNames);
098            }
099    
100            @Override
101            public List<Fields> getFieldsList(
102                            long ddmStructureId, List<String> fieldNames)
103                    throws StorageException {
104    
105                    StorageAdapter storageAdapter = getStructureStorageAdapter(
106                            ddmStructureId);
107    
108                    return storageAdapter.getFieldsList(ddmStructureId, fieldNames);
109            }
110    
111            @Override
112            public List<Fields> getFieldsList(
113                            long ddmStructureId, List<String> fieldNames,
114                            OrderByComparator<Fields> orderByComparator)
115                    throws StorageException {
116    
117                    StorageAdapter storageAdapter = getStructureStorageAdapter(
118                            ddmStructureId);
119    
120                    return storageAdapter.getFieldsList(
121                            ddmStructureId, fieldNames, orderByComparator);
122            }
123    
124            @Override
125            public List<Fields> getFieldsList(
126                            long ddmStructureId, long[] classPKs, List<String> fieldNames,
127                            OrderByComparator<Fields> orderByComparator)
128                    throws StorageException {
129    
130                    StorageAdapter storageAdapter = getStructureStorageAdapter(
131                            ddmStructureId);
132    
133                    return storageAdapter.getFieldsList(
134                            ddmStructureId, classPKs, fieldNames, orderByComparator);
135            }
136    
137            @Override
138            public List<Fields> getFieldsList(
139                            long ddmStructureId, long[] classPKs,
140                            OrderByComparator<Fields> orderByComparator)
141                    throws StorageException {
142    
143                    StorageAdapter storageAdapter = getStructureStorageAdapter(
144                            ddmStructureId);
145    
146                    return storageAdapter.getFieldsList(
147                            ddmStructureId, classPKs, orderByComparator);
148            }
149    
150            @Override
151            public Map<Long, Fields> getFieldsMap(long ddmStructureId, long[] classPKs)
152                    throws StorageException {
153    
154                    StorageAdapter storageAdapter = getStructureStorageAdapter(
155                            ddmStructureId);
156    
157                    return storageAdapter.getFieldsMap(ddmStructureId, classPKs);
158            }
159    
160            @Override
161            public Map<Long, Fields> getFieldsMap(
162                            long ddmStructureId, long[] classPKs, List<String> fieldNames)
163                    throws StorageException {
164    
165                    StorageAdapter storageAdapter = getStructureStorageAdapter(
166                            ddmStructureId);
167    
168                    return storageAdapter.getFieldsMap(
169                            ddmStructureId, classPKs, fieldNames);
170            }
171    
172            @Override
173            public String getStorageType() {
174                    throw new UnsupportedOperationException();
175            }
176    
177            @Override
178            public List<Fields> query(
179                            long ddmStructureId, List<String> fieldNames, Condition condition,
180                            OrderByComparator<Fields> orderByComparator)
181                    throws StorageException {
182    
183                    StorageAdapter storageAdapter = getStructureStorageAdapter(
184                            ddmStructureId);
185    
186                    return storageAdapter.query(
187                            ddmStructureId, fieldNames, condition, orderByComparator);
188            }
189    
190            @Override
191            public int queryCount(long ddmStructureId, Condition condition)
192                    throws StorageException {
193    
194                    StorageAdapter storageAdapter = getStructureStorageAdapter(
195                            ddmStructureId);
196    
197                    return storageAdapter.queryCount(ddmStructureId, condition);
198            }
199    
200            @Override
201            public void update(
202                            long classPK, DDMFormValues ddmFormValues,
203                            ServiceContext serviceContext)
204                    throws StorageException {
205    
206                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
207    
208                    storageAdapter.update(classPK, ddmFormValues, serviceContext);
209            }
210    
211            @Override
212            public void update(
213                            long classPK, Fields fields, boolean mergeFields,
214                            ServiceContext serviceContext)
215                    throws StorageException {
216    
217                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
218    
219                    storageAdapter.update(classPK, fields, mergeFields, serviceContext);
220            }
221    
222            @Override
223            public void update(
224                            long classPK, Fields fields, ServiceContext serviceContext)
225                    throws StorageException {
226    
227                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
228    
229                    storageAdapter.update(classPK, fields, serviceContext);
230            }
231    
232            protected StorageAdapter getClassStorageAdapter(long classPK)
233                    throws StorageException {
234    
235                    try {
236                            DDMStorageLink ddmStorageLink =
237                                    DDMStorageLinkLocalServiceUtil.getClassStorageLink(classPK);
238    
239                            return getStorageAdapter(ddmStorageLink.getStorageType());
240                    }
241                    catch (StorageException se) {
242                            throw se;
243                    }
244                    catch (Exception e) {
245                            throw new StorageException(e);
246                    }
247            }
248    
249            protected StorageAdapter getStorageAdapter(String storageType) {
250                    StorageAdapter storageAdapter =
251                            StorageAdapterRegistryUtil.getStorageAdapter(storageType);
252    
253                    if (storageAdapter != null) {
254                            return storageAdapter;
255                    }
256    
257                    return StorageAdapterRegistryUtil.getDefaultStorageAdapter();
258            }
259    
260            protected StorageAdapter getStructureStorageAdapter(long ddmStructureId)
261                    throws StorageException {
262    
263                    try {
264                            DDMStructure ddmStructure =
265                                    DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
266    
267                            return getStorageAdapter(ddmStructure.getStorageType());
268                    }
269                    catch (StorageException se) {
270                            throw se;
271                    }
272                    catch (Exception e) {
273                            throw new StorageException(e);
274                    }
275            }
276    
277    }