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.service.ServiceContext;
018    import com.liferay.portal.util.PortalUtil;
019    import com.liferay.portlet.dynamicdatamapping.io.DDMFormValuesJSONDeserializerUtil;
020    import com.liferay.portlet.dynamicdatamapping.io.DDMFormValuesJSONSerializerUtil;
021    import com.liferay.portlet.dynamicdatamapping.model.DDMContent;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMStorageLink;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
024    import com.liferay.portlet.dynamicdatamapping.service.DDMContentLocalServiceUtil;
025    import com.liferay.portlet.dynamicdatamapping.service.DDMStorageLinkLocalServiceUtil;
026    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Pablo Carvalho
032     */
033    public class JSONStorageAdapter extends BaseStorageAdapter {
034    
035            @Override
036            public long doCreate(
037                            long companyId, long ddmStructureId, DDMFormValues ddmFormValues,
038                            ServiceContext serviceContext)
039                    throws Exception {
040    
041                    long classNameId = PortalUtil.getClassNameId(
042                            DDMContent.class.getName());
043    
044                    String serializedDDMFormValues =
045                            DDMFormValuesJSONSerializerUtil.serialize(ddmFormValues);
046    
047                    DDMContent ddmContent = DDMContentLocalServiceUtil.addContent(
048                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
049                            DDMStorageLink.class.getName(), null, serializedDDMFormValues,
050                            serviceContext);
051    
052                    DDMStorageLinkLocalServiceUtil.addStorageLink(
053                            classNameId, ddmContent.getPrimaryKey(), ddmStructureId,
054                            serviceContext);
055    
056                    return ddmContent.getPrimaryKey();
057            }
058    
059            @Override
060            public void doUpdate(
061                            long classPK, DDMFormValues ddmFormValues,
062                            ServiceContext serviceContext)
063                    throws Exception {
064    
065                    DDMContent ddmContent = DDMContentLocalServiceUtil.getContent(classPK);
066    
067                    ddmContent.setModifiedDate(serviceContext.getModifiedDate(null));
068    
069                    String serializedDDMFormValues =
070                            DDMFormValuesJSONSerializerUtil.serialize(ddmFormValues);
071    
072                    ddmContent.setData(serializedDDMFormValues);
073    
074                    DDMContentLocalServiceUtil.updateContent(
075                            ddmContent.getPrimaryKey(), ddmContent.getName(),
076                            ddmContent.getDescription(), ddmContent.getData(), serviceContext);
077            }
078    
079            @Override
080            public String getStorageType() {
081                    return StorageType.JSON.toString();
082            }
083    
084            @Override
085            protected void doDeleteByClass(long classPK) throws Exception {
086                    DDMContentLocalServiceUtil.deleteDDMContent(classPK);
087    
088                    DDMStorageLinkLocalServiceUtil.deleteClassStorageLink(classPK);
089            }
090    
091            @Override
092            protected void doDeleteByDDMStructure(long ddmStructureId)
093                    throws Exception {
094    
095                    List<DDMStorageLink> ddmStorageLinks =
096                            DDMStorageLinkLocalServiceUtil.getStructureStorageLinks(
097                                    ddmStructureId);
098    
099                    for (DDMStorageLink ddmStorageLink : ddmStorageLinks) {
100                            doDeleteByClass(ddmStorageLink.getClassPK());
101                    }
102            }
103    
104            @Override
105            protected DDMFormValues doGetDDMFormValues(long classPK) throws Exception {
106                    DDMContent ddmContent = DDMContentLocalServiceUtil.getContent(classPK);
107    
108                    DDMStorageLink ddmStorageLink =
109                            DDMStorageLinkLocalServiceUtil.getClassStorageLink(
110                                    ddmContent.getPrimaryKey());
111    
112                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(
113                            ddmStorageLink.getStructureId());
114    
115                    DDMFormValues ddmFormValues =
116                            DDMFormValuesJSONDeserializerUtil.deserialize(
117                                    ddmStructure.getDDMForm(), ddmContent.getData());
118    
119                    return ddmFormValues;
120            }
121    
122    }