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