001    /**
002     * Copyright (c) 2000-2012 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.dynamicdatalists.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.upload.UploadPortletRequest;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextFactory;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.documentlibrary.FileSizeException;
032    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
033    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
034    import com.liferay.portlet.dynamicdatalists.util.DDLUtil;
035    import com.liferay.portlet.dynamicdatamapping.storage.Field;
036    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
037    
038    import java.io.Serializable;
039    
040    import java.util.List;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.PortletRequest;
046    import javax.portlet.ResourceRequest;
047    import javax.portlet.ResourceResponse;
048    
049    import org.apache.struts.action.ActionForm;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Bruno Basto
054     */
055    public class EditRecordFileAction extends PortletAction {
056    
057            @Override
058            public void processAction(
059                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
060                            ActionRequest actionRequest, ActionResponse actionResponse)
061                    throws Exception {
062    
063                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
064    
065                    if (cmd.equals(Constants.DELETE)) {
066                            deleteRecordFieldFile(actionRequest);
067                    }
068    
069                    sendRedirect(actionRequest, actionResponse);
070            }
071    
072            @Override
073            public void serveResource(
074                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
075                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
076                    throws Exception {
077    
078                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
079    
080                    try {
081                            jsonObject = updateRecordFieldFile(resourceRequest);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof FileSizeException) {
085                                    jsonObject.put("exception", e.toString());
086                            }
087                            else {
088                                    throw e;
089                            }
090                    }
091    
092                    writeJSON(resourceRequest, resourceResponse, jsonObject);
093            }
094    
095            protected void deleteRecordFieldFile(PortletRequest portletRequest)
096                    throws PortalException, SystemException {
097    
098                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
099                            WebKeys.THEME_DISPLAY);
100    
101                    long recordId = ParamUtil.getLong(portletRequest, "recordId");
102    
103                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
104    
105                    Fields fields = record.getFields();
106    
107                    String fieldName = ParamUtil.getString(portletRequest, "fieldName");
108    
109                    Field field = fields.get(fieldName);
110    
111                    List<Serializable> values = field.getValues(themeDisplay.getLocale());
112    
113                    int valueIndex = ParamUtil.getInteger(portletRequest, "valueIndex");
114    
115                    values.remove(valueIndex);
116    
117                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
118                            DDLRecord.class.getName(), portletRequest);
119    
120                    DDLRecordLocalServiceUtil.updateRecord(
121                            themeDisplay.getUserId(), recordId, false, record.getDisplayIndex(),
122                            fields, true, serviceContext);
123            }
124    
125            @Override
126            protected boolean isCheckMethodOnProcessAction() {
127                    return _CHECK_METHOD_ON_PROCESS_ACTION;
128            }
129    
130            protected JSONObject updateRecordFieldFile(PortletRequest request)
131                    throws Exception {
132    
133                    UploadPortletRequest uploadPortletRequest =
134                            PortalUtil.getUploadPortletRequest(request);
135    
136                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
137                            DDLRecord.class.getName(), uploadPortletRequest);
138    
139                    long recordId = ParamUtil.getLong(serviceContext, "recordId");
140    
141                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
142    
143                    String fieldName = ParamUtil.getString(serviceContext, "fieldName");
144    
145                    DDLUtil.uploadRecordFieldFile(record, fieldName, serviceContext);
146    
147                    String fieldValue = String.valueOf(record.getFieldValue(fieldName));
148    
149                    if (Validator.isNull(fieldValue)) {
150                            fieldValue = JSONFactoryUtil.getNullJSON();
151                    }
152    
153                    return JSONFactoryUtil.createJSONObject(fieldValue);
154            }
155    
156            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
157    
158    }