1
22
23 package com.liferay.portlet.imagegallery.action;
24
25 import com.liferay.portal.kernel.portlet.LiferayWindowState;
26 import com.liferay.portal.kernel.util.Constants;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.ParamUtil;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.security.auth.PrincipalException;
32 import com.liferay.portal.struts.PortletAction;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.portlet.imagegallery.ImageNameException;
35 import com.liferay.portlet.imagegallery.ImageSizeException;
36 import com.liferay.portlet.imagegallery.NoSuchFolderException;
37 import com.liferay.portlet.imagegallery.NoSuchImageException;
38 import com.liferay.portlet.imagegallery.model.IGImage;
39 import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
40 import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
41 import com.liferay.portlet.tags.TagsEntryException;
42 import com.liferay.util.FileUtil;
43 import com.liferay.util.servlet.SessionErrors;
44 import com.liferay.util.servlet.UploadPortletRequest;
45
46 import java.io.File;
47
48 import javax.portlet.ActionRequest;
49 import javax.portlet.ActionResponse;
50 import javax.portlet.PortletConfig;
51 import javax.portlet.RenderRequest;
52 import javax.portlet.RenderResponse;
53
54 import org.apache.struts.action.ActionForm;
55 import org.apache.struts.action.ActionForward;
56 import org.apache.struts.action.ActionMapping;
57
58
64 public class EditImageAction extends PortletAction {
65
66 public void processAction(
67 ActionMapping mapping, ActionForm form, PortletConfig config,
68 ActionRequest req, ActionResponse res)
69 throws Exception {
70
71 String cmd = ParamUtil.getString(req, Constants.CMD);
72
73 try {
74 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
75 updateImage(req);
76 }
77 else if (cmd.equals(Constants.DELETE)) {
78 deleteImage(req);
79
80 sendRedirect(req, res);
81 }
82 }
83 catch (Exception e) {
84 if (e instanceof NoSuchImageException ||
85 e instanceof PrincipalException) {
86
87 SessionErrors.add(req, e.getClass().getName());
88
89 setForward(req, "portlet.image_gallery.error");
90 }
91 else if (e instanceof ImageNameException ||
92 e instanceof ImageSizeException ||
93 e instanceof NoSuchFolderException) {
94
95 SessionErrors.add(req, e.getClass().getName());
96 }
97 else if (e instanceof TagsEntryException) {
98 SessionErrors.add(req, e.getClass().getName(), e);
99 }
100 else {
101 throw e;
102 }
103 }
104 }
105
106 public ActionForward render(
107 ActionMapping mapping, ActionForm form, PortletConfig config,
108 RenderRequest req, RenderResponse res)
109 throws Exception {
110
111 try {
112 ActionUtil.getImage(req);
113 }
114 catch (Exception e) {
115 if (e instanceof NoSuchImageException ||
116 e instanceof PrincipalException) {
117
118 SessionErrors.add(req, e.getClass().getName());
119
120 return mapping.findForward("portlet.image_gallery.error");
121 }
122 else {
123 throw e;
124 }
125 }
126
127 String forward = "portlet.image_gallery.edit_image";
128
129 if (req.getWindowState().equals(LiferayWindowState.POP_UP)) {
130 forward = "portlet.image_gallery.edit_image_form";
131 }
132
133 return mapping.findForward(getForward(req, forward));
134 }
135
136 protected void deleteImage(ActionRequest req) throws Exception {
137 long imageId = ParamUtil.getLong(req, "imageId");
138
139 IGImageServiceUtil.deleteImage(imageId);
140 }
141
142 protected String getContentType(UploadPortletRequest uploadReq, File file) {
143 String contentType = GetterUtil.getString(
144 uploadReq.getContentType("file"));
145
146 if (contentType.equals("application/octet-stream")) {
147 String ext = GetterUtil.getString(
148 FileUtil.getExtension(file.getName())).toLowerCase();
149
150 if (Validator.isNotNull(ext)) {
151 if (ext.equals("jpg")) {
152 ext = "jpeg";
153 }
154 else if (ext.equals("tif")) {
155 ext = "tiff";
156 }
157
158 contentType = "image/" + ext;
159 }
160 }
161
162 return contentType;
163 }
164
165 protected void updateImage(ActionRequest req) throws Exception {
166 UploadPortletRequest uploadReq =
167 PortalUtil.getUploadPortletRequest(req);
168
169 long imageId = ParamUtil.getLong(uploadReq, "imageId");
170
171 long folderId = ParamUtil.getLong(uploadReq, "folderId");
172 String description = ParamUtil.getString(uploadReq, "description");
173
174 if (Validator.isNull(description)) {
175 description = uploadReq.getFileName("file");
176 }
177
178 File file = uploadReq.getFile("file");
179 String contentType = getContentType(uploadReq, file);
180
181 if (contentType.equals("application/octet-stream")) {
182 String ext = GetterUtil.getString(
183 FileUtil.getExtension(file.getName())).toLowerCase();
184
185 if (Validator.isNotNull(ext)) {
186 if (ext.equals("jpg")) {
187 ext = "jpeg";
188 }
189 else if (ext.equals("tif")) {
190 ext = "tiff";
191 }
192
193 contentType = "image/" + ext;
194 }
195 }
196
197 String[] tagsEntries = StringUtil.split(
198 ParamUtil.getString(uploadReq, "tagsEntries"));
199
200 String[] communityPermissions = req.getParameterValues(
201 "communityPermissions");
202 String[] guestPermissions = req.getParameterValues(
203 "guestPermissions");
204
205 if (imageId <= 0) {
206
207
209 IGImage image = IGImageServiceUtil.addImage(
210 folderId, description, file, contentType, tagsEntries,
211 communityPermissions, guestPermissions);
212
213 AssetPublisherUtil.addAndStoreSelection(
214 req, IGImage.class.getName(), image.getImageId(), -1);
215 }
216 else {
217
218
220 String fileName = uploadReq.getFileName("file");
221
222 if (Validator.isNull(fileName)) {
223 file = null;
224 }
225
226 IGImageServiceUtil.updateImage(
227 imageId, folderId, description, file, contentType, tagsEntries);
228 }
229 }
230
231 }