001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.upload.UploadPortletRequest;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.documentlibrary.FileSizeException;
026    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
027    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
028    import com.liferay.portlet.dynamicdatalists.util.DDLUtil;
029    
030    import javax.portlet.PortletConfig;
031    import javax.portlet.ResourceRequest;
032    import javax.portlet.ResourceResponse;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Bruno Basto
039     */
040    public class EditRecordFileAction extends PortletAction {
041    
042            @Override
043            public void serveResource(
044                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
045                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
046                    throws Exception {
047    
048                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
049    
050                    try {
051                            jsonObject = uploadRecordFieldFile(resourceRequest);
052                    }
053                    catch (Exception e) {
054                            if (e instanceof FileSizeException) {
055                                    jsonObject.put("exception", e.toString());
056                            }
057                            else {
058                                    throw e;
059                            }
060                    }
061    
062                    writeJSON(resourceRequest, resourceResponse, jsonObject);
063            }
064    
065            protected JSONObject uploadRecordFieldFile(ResourceRequest resourceRequest)
066                    throws Exception {
067    
068                    long recordId = ParamUtil.getLong(resourceRequest, "recordId");
069    
070                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(recordId);
071    
072                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
073                            DDLRecord.class.getName(), resourceRequest);
074    
075                    UploadPortletRequest uploadPortletRequest =
076                            PortalUtil.getUploadPortletRequest(resourceRequest);
077    
078                    DDLUtil.uploadRecordFiles(record, uploadPortletRequest, serviceContext);
079    
080                    String fieldName = ParamUtil.getString(resourceRequest, "fieldName");
081    
082                    String fieldValue = String.valueOf(record.getFieldValue(fieldName));
083    
084                    return JSONFactoryUtil.createJSONObject(fieldValue);
085            }
086    
087    }