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.spring.osgi.OSGiBeanProperties;
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    
025    /**
026     * @author Eduardo Lundgren
027     */
028    @OSGiBeanProperties(service = StorageEngine.class)
029    public class StorageEngineImpl implements StorageEngine {
030    
031            @Override
032            public long create(
033                            long companyId, long ddmStructureId, DDMFormValues ddmFormValues,
034                            ServiceContext serviceContext)
035                    throws StorageException {
036    
037                    StorageAdapter storageAdapter = getStructureStorageAdapter(
038                            ddmStructureId);
039    
040                    return storageAdapter.create(
041                            companyId, ddmStructureId, ddmFormValues, serviceContext);
042            }
043    
044            @Override
045            public void deleteByClass(long classPK) throws StorageException {
046                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
047    
048                    storageAdapter.deleteByClass(classPK);
049            }
050    
051            @Override
052            public void deleteByDDMStructure(long ddmStructureId)
053                    throws StorageException {
054    
055                    StorageAdapter storageAdapter = getStructureStorageAdapter(
056                            ddmStructureId);
057    
058                    storageAdapter.deleteByDDMStructure(ddmStructureId);
059            }
060    
061            @Override
062            public DDMFormValues getDDMFormValues(long classPK)
063                    throws StorageException {
064    
065                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
066    
067                    return storageAdapter.getDDMFormValues(classPK);
068            }
069    
070            @Override
071            public String getStorageType() {
072                    throw new UnsupportedOperationException();
073            }
074    
075            @Override
076            public void update(
077                            long classPK, DDMFormValues ddmFormValues,
078                            ServiceContext serviceContext)
079                    throws StorageException {
080    
081                    StorageAdapter storageAdapter = getClassStorageAdapter(classPK);
082    
083                    storageAdapter.update(classPK, ddmFormValues, serviceContext);
084            }
085    
086            protected StorageAdapter getClassStorageAdapter(long classPK)
087                    throws StorageException {
088    
089                    try {
090                            DDMStorageLink ddmStorageLink =
091                                    DDMStorageLinkLocalServiceUtil.getClassStorageLink(classPK);
092    
093                            return getStorageAdapter(ddmStorageLink.getStorageType());
094                    }
095                    catch (StorageException se) {
096                            throw se;
097                    }
098                    catch (Exception e) {
099                            throw new StorageException(e);
100                    }
101            }
102    
103            protected StorageAdapter getStorageAdapter(String storageType) {
104                    StorageAdapter storageAdapter =
105                            StorageAdapterRegistryUtil.getStorageAdapter(storageType);
106    
107                    if (storageAdapter != null) {
108                            return storageAdapter;
109                    }
110    
111                    return StorageAdapterRegistryUtil.getDefaultStorageAdapter();
112            }
113    
114            protected StorageAdapter getStructureStorageAdapter(long ddmStructureId)
115                    throws StorageException {
116    
117                    try {
118                            DDMStructure ddmStructure =
119                                    DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
120    
121                            return getStorageAdapter(ddmStructure.getStorageType());
122                    }
123                    catch (StorageException se) {
124                            throw se;
125                    }
126                    catch (Exception e) {
127                            throw new StorageException(e);
128                    }
129            }
130    
131    }