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