001    /**
002     * Copyright (c) 2000-2011 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,
057                            PortletConfig portletConfig, ActionRequest actionRequest,
058                            ActionResponse actionResponse)
059                    throws Exception {
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    if (cmd.equals(Constants.DELETE)) {
064                            deleteRecordFieldFile(actionRequest);
065                    }
066    
067                    sendRedirect(actionRequest, actionResponse);
068            }
069    
070            @Override
071            public void serveResource(
072                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
073                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
074                    throws Exception {
075    
076                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
077    
078                    try {
079                            jsonObject = updateRecordFieldFile(resourceRequest);
080                    }
081                    catch (Exception e) {
082                            if (e instanceof FileSizeException) {
083                                    jsonObject.put("exception", e.toString());
084                            }
085                            else {
086                                    throw e;
087                            }
088                    }
089    
090                    writeJSON(resourceRequest, resourceResponse, jsonObject);
091            }
092    
093            protected void deleteRecordFieldFile(PortletRequest portletRequest)
094                    throws PortalException, SystemException {
095    
096                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
097                            WebKeys.THEME_DISPLAY);
098    
099                    long recordId = ParamUtil.getLong(portletRequest, "recordId");
100    
101                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
102    
103                    Fields fields = record.getFields();
104    
105                    String fieldName = ParamUtil.getString(portletRequest, "fieldName");
106    
107                    Field field = fields.get(fieldName);
108    
109                    field.setValue(StringPool.BLANK);
110    
111                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
112                            DDLRecord.class.getName(), portletRequest);
113    
114                    DDLRecordLocalServiceUtil.updateRecord(
115                            themeDisplay.getUserId(), recordId, false, record.getDisplayIndex(),
116                            fields, true, serviceContext);
117            }
118    
119            @Override
120            protected boolean isCheckMethodOnProcessAction() {
121                    return _CHECK_METHOD_ON_PROCESS_ACTION;
122            }
123    
124            protected JSONObject updateRecordFieldFile(PortletRequest request)
125                    throws Exception {
126    
127                    long recordId = ParamUtil.getLong(request, "recordId");
128    
129                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
130    
131                    String fieldName = ParamUtil.getString(request, "fieldName");
132    
133                    UploadPortletRequest uploadPortletRequest =
134                            PortalUtil.getUploadPortletRequest(request);
135    
136                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
137                            DDLRecord.class.getName(), request);
138    
139                    DDLUtil.uploadRecordFieldFile(
140                            record, fieldName, uploadPortletRequest, serviceContext);
141    
142                    String fieldValue = String.valueOf(record.getFieldValue(fieldName));
143    
144                    if (Validator.isNull(fieldValue)) {
145                            fieldValue = JSONFactoryUtil.getNullJSON();
146                    }
147    
148                    return JSONFactoryUtil.createJSONObject(fieldValue);
149            }
150    
151            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
152    
153    }