001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
162                    StorageAdapter defaultStorageAdapter) {
163    
164                    _defaultStorageAdapter = defaultStorageAdapter;
165            }
166    
167            public void setStorageAdapters(
168                    Map<String, StorageAdapter> storageAdapters) {
169    
170                    _storageAdapters = storageAdapters;
171            }
172    
173            public void update(
174                            long classPK, Fields fields, boolean mergeFields,
175                            ServiceContext serviceContext)
176                    throws StorageException {
177    
178                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
179    
180                    storageAdapter.update(classPK, fields, mergeFields, serviceContext);
181            }
182    
183            public void update(
184                            long classPK, Fields fields, ServiceContext serviceContext)
185                    throws StorageException {
186    
187                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
188    
189                    storageAdapter.update(classPK, fields, serviceContext);
190            }
191    
192            protected StorageAdapter getClassStorageAdapter(long classPK)
193                    throws StorageException {
194    
195                    try {
196                            DDMStorageLink ddmStorageLink =
197                                    DDMStorageLinkLocalServiceUtil.getClassStorageLink(classPK);
198    
199                            return getStorageAdapter(ddmStorageLink.getStorageType());
200                    }
201                    catch (StorageException se) {
202                            throw se;
203                    }
204                    catch (Exception e) {
205                            throw new StorageException(e);
206                    }
207            }
208    
209            protected StorageAdapter getStorageAdapter(String storageType) {
210                    StorageAdapter storageAdapter = _storageAdapters.get(storageType);
211    
212                    if (storageAdapter == null) {
213                            storageAdapter = _defaultStorageAdapter;
214                    }
215    
216                    return storageAdapter;
217            }
218    
219            protected StorageAdapter getStructureStorageAdapter(long ddmStructureId)
220                    throws StorageException {
221    
222                    try {
223                            DDMStructure ddmStructure =
224                                    DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
225    
226                            return getStorageAdapter(ddmStructure.getStorageType());
227                    }
228                    catch (StorageException se) {
229                            throw se;
230                    }
231                    catch (Exception e) {
232                            throw new StorageException(e);
233                    }
234            }
235    
236            private StorageAdapter _defaultStorageAdapter;
237            private Map<String, StorageAdapter> _storageAdapters =
238                    new HashMap<String, StorageAdapter>();
239    
240    }