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