001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
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 actionMapping, ActionForm actionForm,
073                            PortletConfig portletConfig, ResourceRequest resourceRequest,
074                            ResourceResponse resourceResponse)
075                    throws Exception {
076    
077                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
078    
079                    try {
080                            jsonObject = updateRecordFieldFile(resourceRequest);
081                    }
082                    catch (Exception e) {
083                            if (e instanceof FileSizeException) {
084                                    jsonObject.put("exception", e.toString());
085                            }
086                            else {
087                                    throw e;
088                            }
089                    }
090    
091                    writeJSON(resourceRequest, resourceResponse, jsonObject);
092            }
093    
094            protected void deleteRecordFieldFile(PortletRequest portletRequest)
095                    throws PortalException, SystemException {
096    
097                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
098                            WebKeys.THEME_DISPLAY);
099    
100                    long recordId = ParamUtil.getLong(portletRequest, "recordId");
101    
102                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
103    
104                    Fields fields = record.getFields();
105    
106                    String fieldName = ParamUtil.getString(portletRequest, "fieldName");
107    
108                    Field field = fields.get(fieldName);
109    
110                    field.setValue(StringPool.BLANK);
111    
112                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
113                            DDLRecord.class.getName(), portletRequest);
114    
115                    DDLRecordLocalServiceUtil.updateRecord(
116                            themeDisplay.getUserId(), recordId, false, record.getDisplayIndex(),
117                            fields, true, serviceContext);
118            }
119    
120            @Override
121            protected boolean isCheckMethodOnProcessAction() {
122                    return _CHECK_METHOD_ON_PROCESS_ACTION;
123            }
124    
125            protected JSONObject updateRecordFieldFile(PortletRequest request)
126                    throws Exception {
127    
128                    UploadPortletRequest uploadPortletRequest =
129                            PortalUtil.getUploadPortletRequest(request);
130    
131                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
132                            DDLRecord.class.getName(), uploadPortletRequest);
133    
134                    long recordId = ParamUtil.getLong(serviceContext, "recordId");
135    
136                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
137    
138                    String fieldName = ParamUtil.getString(serviceContext, "fieldName");
139    
140                    DDLUtil.uploadRecordFieldFile(record, fieldName, 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    }