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.portal.util.PortalUtil;
020    import com.liferay.portlet.dynamicdatamapping.StorageException;
021    import com.liferay.portlet.dynamicdatamapping.io.DDMFormValuesJSONDeserializerUtil;
022    import com.liferay.portlet.dynamicdatamapping.io.DDMFormValuesJSONSerializerUtil;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMContent;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMStorageLink;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
026    import com.liferay.portlet.dynamicdatamapping.service.DDMContentLocalServiceUtil;
027    import com.liferay.portlet.dynamicdatamapping.service.DDMStorageLinkLocalServiceUtil;
028    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
029    import com.liferay.portlet.dynamicdatamapping.storage.query.Condition;
030    import com.liferay.portlet.dynamicdatamapping.util.DDMFormValuesToFieldsConverterUtil;
031    import com.liferay.portlet.dynamicdatamapping.util.FieldsToDDMFormValuesConverterUtil;
032    
033    import java.util.HashMap;
034    import java.util.Iterator;
035    import java.util.List;
036    import java.util.Map;
037    
038    /**
039     * @author Pablo Carvalho
040     */
041    public class JSONStorageAdapter extends BaseStorageAdapter {
042    
043            @Override
044            public String getStorageType() {
045                    return StorageType.JSON.toString();
046            }
047    
048            @Override
049            protected long doCreate(
050                            long companyId, long ddmStructureId, Fields fields,
051                            ServiceContext serviceContext)
052                    throws Exception {
053    
054                    DDMStructure ddmStructure =
055                            DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
056    
057                    DDMFormValues ddmFormValues =
058                            FieldsToDDMFormValuesConverterUtil.convert(ddmStructure, fields);
059    
060                    return _doCreate(
061                            companyId, ddmStructureId, ddmFormValues, serviceContext);
062            }
063    
064            @Override
065            protected void doDeleteByClass(long classPK) throws Exception {
066                    DDMContentLocalServiceUtil.deleteDDMContent(classPK);
067    
068                    DDMStorageLinkLocalServiceUtil.deleteClassStorageLink(classPK);
069            }
070    
071            @Override
072            protected void doDeleteByDDMStructure(long ddmStructureId)
073                    throws Exception {
074    
075                    throw new UnsupportedOperationException();
076            }
077    
078            @Override
079            protected List<Fields> doGetFieldsListByClasses(
080                            long ddmStructureId, long[] classPKs, List<String> fieldNames,
081                            OrderByComparator<Fields> orderByComparator)
082                    throws Exception {
083    
084                    throw new UnsupportedOperationException();
085            }
086    
087            @Override
088            protected List<Fields> doGetFieldsListByDDMStructure(
089                            long ddmStructureId, List<String> fieldNames,
090                            OrderByComparator<Fields> orderByComparator)
091                    throws Exception {
092    
093                    throw new UnsupportedOperationException();
094            }
095    
096            @Override
097            protected Map<Long, Fields> doGetFieldsMapByClasses(
098                            long ddmStructureId, long[] classPKs, List<String> fieldNames)
099                    throws Exception {
100    
101                    Map<Long, Fields> fieldsMapByClasses = new HashMap<Long, Fields>();
102    
103                    for (long classPK : classPKs) {
104                            fieldsMapByClasses.put(classPK, _getFields(classPK, fieldNames));
105                    }
106    
107                    return fieldsMapByClasses;
108            }
109    
110            @Override
111            protected List<Fields> doQuery(
112                            long ddmStructureId, List<String> fieldNames, Condition condition,
113                            OrderByComparator<Fields> orderByComparator)
114                    throws Exception {
115    
116                    throw new UnsupportedOperationException();
117            }
118    
119            @Override
120            protected int doQueryCount(long ddmStructureId, Condition condition)
121                    throws Exception {
122    
123                    throw new UnsupportedOperationException();
124            }
125    
126            @Override
127            protected void doUpdate(
128                            long classPK, Fields fields, boolean mergeFields,
129                            ServiceContext serviceContext)
130                    throws Exception {
131    
132                    long ddmStructureId = fields.getDDMStructureId();
133    
134                    DDMStructure ddmStructure =
135                            DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
136    
137                    DDMFormValues ddmFormValues =
138                            FieldsToDDMFormValuesConverterUtil.convert(ddmStructure, fields);
139    
140                    _doUpdate(classPK, ddmFormValues, serviceContext);
141            }
142    
143            private long _doCreate(
144                            long companyId, long ddmStructureId, DDMFormValues ddmFormValues,
145                            ServiceContext serviceContext)
146                    throws Exception {
147    
148                    long classNameId = PortalUtil.getClassNameId(
149                            DDMContent.class.getName());
150    
151                    String serializedDDMFormValues =
152                            DDMFormValuesJSONSerializerUtil.serialize(ddmFormValues);
153    
154                    DDMContent ddmContent = DDMContentLocalServiceUtil.addContent(
155                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
156                            DDMStorageLink.class.getName(), null, serializedDDMFormValues,
157                            serviceContext);
158    
159                    DDMStorageLinkLocalServiceUtil.addStorageLink(
160                            classNameId, ddmContent.getPrimaryKey(), ddmStructureId,
161                            serviceContext);
162    
163                    return ddmContent.getPrimaryKey();
164            }
165    
166            private void _doUpdate(
167                            long classPK, DDMFormValues ddmFormValues,
168                            ServiceContext serviceContext)
169                    throws Exception {
170    
171                    DDMContent ddmContent = DDMContentLocalServiceUtil.getContent(classPK);
172    
173                    ddmContent.setModifiedDate(serviceContext.getModifiedDate(null));
174    
175                    String serializedDDMFormValues =
176                            DDMFormValuesJSONSerializerUtil.serialize(ddmFormValues);
177    
178                    ddmContent.setData(serializedDDMFormValues);
179    
180                    DDMContentLocalServiceUtil.updateContent(
181                            ddmContent.getPrimaryKey(), ddmContent.getName(),
182                            ddmContent.getDescription(), ddmContent.getData(), serviceContext);
183            }
184    
185            private Fields _getFields(long classPK) throws StorageException {
186                    try {
187                            DDMContent ddmContent = DDMContentLocalServiceUtil.getContent(
188                                    classPK);
189    
190                            DDMStorageLink ddmStorageLink =
191                                    DDMStorageLinkLocalServiceUtil.getClassStorageLink(
192                                            ddmContent.getPrimaryKey());
193    
194                            DDMStructure ddmStructure =
195                                    DDMStructureLocalServiceUtil.getStructure(
196                                            ddmStorageLink.getStructureId());
197    
198                            DDMFormValues ddmFormValues =
199                                    DDMFormValuesJSONDeserializerUtil.deserialize(
200                                            ddmContent.getData());
201    
202                            return DDMFormValuesToFieldsConverterUtil.convert(
203                                    ddmStructure, ddmFormValues);
204                    }
205                    catch (Exception e) {
206                            throw new StorageException(e);
207                    }
208            }
209    
210            private Fields _getFields(long classPK, List<String> fieldNames)
211                    throws StorageException {
212    
213                    Fields fields = _getFields(classPK);
214    
215                    if (fieldNames == null) {
216                            return fields;
217                    }
218    
219                    Iterator<Field> itr = fields.iterator();
220    
221                    while (itr.hasNext()) {
222                            Field field = itr.next();
223    
224                            if (!fieldNames.contains(field.getName())) {
225                                    itr.remove();
226                            }
227                    }
228    
229                    return fields;
230            }
231    
232    }