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