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.exception.PortalException;
018    import com.liferay.portal.service.ServiceContext;
019    import com.liferay.portlet.dynamicdatamapping.StorageException;
020    import com.liferay.portlet.dynamicdatamapping.util.DDMFormValuesTransformer;
021    import com.liferay.portlet.dynamicdatamapping.util.DocumentLibraryDDMFormFieldValueTransformer;
022    import com.liferay.portlet.dynamicdatamapping.util.HTMLSanitizerDDMFormFieldValueTransformer;
023    import com.liferay.portlet.dynamicdatamapping.validator.DDMFormValuesValidatorUtil;
024    
025    /**
026     * @author Eduardo Lundgren
027     * @author Brian Wing Shun Chan
028     * @author Marcellus Tavares
029     */
030    public abstract class BaseStorageAdapter implements StorageAdapter {
031    
032            @Override
033            public long create(
034                            long companyId, long ddmStructureId, DDMFormValues ddmFormValues,
035                            ServiceContext serviceContext)
036                    throws StorageException {
037    
038                    try {
039                            validateDDMFormValues(ddmFormValues);
040    
041                            transformDDMFormValues(ddmFormValues, serviceContext);
042    
043                            return doCreate(
044                                    companyId, ddmStructureId, ddmFormValues, serviceContext);
045                    }
046                    catch (StorageException se) {
047                            throw se;
048                    }
049                    catch (Exception e) {
050                            throw new StorageException(e);
051                    }
052            }
053    
054            @Override
055            public void deleteByClass(long classPK) throws StorageException {
056                    try {
057                            doDeleteByClass(classPK);
058                    }
059                    catch (StorageException se) {
060                            throw se;
061                    }
062                    catch (Exception e) {
063                            throw new StorageException(e);
064                    }
065            }
066    
067            @Override
068            public void deleteByDDMStructure(long ddmStructureId)
069                    throws StorageException {
070    
071                    try {
072                            doDeleteByDDMStructure(ddmStructureId);
073                    }
074                    catch (StorageException se) {
075                            throw se;
076                    }
077                    catch (Exception e) {
078                            throw new StorageException(e);
079                    }
080            }
081    
082            @Override
083            public DDMFormValues getDDMFormValues(long classPK)
084                    throws StorageException {
085    
086                    try {
087                            return doGetDDMFormValues(classPK);
088                    }
089                    catch (StorageException se) {
090                            throw se;
091                    }
092                    catch (Exception e) {
093                            throw new StorageException(e);
094                    }
095            }
096    
097            @Override
098            public void update(
099                            long classPK, DDMFormValues ddmFormValues,
100                            ServiceContext serviceContext)
101                    throws StorageException {
102    
103                    try {
104                            validateDDMFormValues(ddmFormValues);
105    
106                            transformDDMFormValues(ddmFormValues, serviceContext);
107    
108                            doUpdate(classPK, ddmFormValues, serviceContext);
109                    }
110                    catch (StorageException se) {
111                            throw se;
112                    }
113                    catch (Exception e) {
114                            throw new StorageException(e);
115                    }
116            }
117    
118            protected abstract long doCreate(
119                            long companyId, long ddmStructureId, DDMFormValues ddmFormValues,
120                            ServiceContext serviceContext)
121                    throws Exception;
122    
123            protected abstract void doDeleteByClass(long classPK) throws Exception;
124    
125            protected abstract void doDeleteByDDMStructure(long ddmStructureId)
126                    throws Exception;
127    
128            protected abstract DDMFormValues doGetDDMFormValues(long classPK)
129                    throws Exception;
130    
131            protected abstract void doUpdate(
132                            long classPK, DDMFormValues ddmFormValues,
133                            ServiceContext serviceContext)
134                    throws Exception;
135    
136            protected void transformDDMFormValues(
137                            DDMFormValues ddmFormValues, ServiceContext serviceContext)
138                    throws PortalException {
139    
140                    DDMFormValuesTransformer ddmFormValuesTransformer =
141                            new DDMFormValuesTransformer(ddmFormValues);
142    
143                    ddmFormValuesTransformer.addTransformer(
144                            new DocumentLibraryDDMFormFieldValueTransformer());
145    
146                    ddmFormValuesTransformer.addTransformer(
147                            new HTMLSanitizerDDMFormFieldValueTransformer(
148                                    serviceContext.getCompanyId(), serviceContext.getScopeGroupId(),
149                                    serviceContext.getUserId()));
150    
151                    ddmFormValuesTransformer.transform();
152            }
153    
154            protected void validateDDMFormValues(DDMFormValues ddmFormValues)
155                    throws PortalException {
156    
157                    DDMFormValuesValidatorUtil.validate(ddmFormValues);
158            }
159    
160    }