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