001
014
015 package com.liferay.portlet.journal.action;
016
017 import com.liferay.portal.kernel.language.LanguageUtil;
018 import com.liferay.portal.kernel.upload.UploadServletRequest;
019 import com.liferay.portal.kernel.util.Constants;
020 import com.liferay.portal.kernel.util.FileUtil;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.kernel.xml.Document;
025 import com.liferay.portal.kernel.xml.Element;
026 import com.liferay.portal.kernel.xml.SAXReaderUtil;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.service.ImageLocalServiceUtil;
029 import com.liferay.portal.theme.ThemeDisplay;
030 import com.liferay.portal.util.PortalUtil;
031 import com.liferay.portal.util.WebKeys;
032 import com.liferay.portal.webserver.WebServerServletTokenUtil;
033 import com.liferay.portlet.journal.model.JournalArticle;
034 import com.liferay.portlet.journal.model.JournalArticleConstants;
035 import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
036 import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
037 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
038 import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
039 import com.liferay.portlet.journal.util.JournalUtil;
040 import com.liferay.util.PwdGenerator;
041
042 import java.io.File;
043
044 import java.util.Date;
045 import java.util.Iterator;
046 import java.util.Map;
047
048 import javax.servlet.http.HttpServletRequest;
049 import javax.servlet.http.HttpServletResponse;
050
051 import org.apache.struts.action.Action;
052 import org.apache.struts.action.ActionForm;
053 import org.apache.struts.action.ActionForward;
054 import org.apache.struts.action.ActionMapping;
055
056
060 public class ViewArticleContentAction extends Action {
061
062 @Override
063 public ActionForward execute(
064 ActionMapping actionMapping, ActionForm actionForm,
065 HttpServletRequest request, HttpServletResponse response)
066 throws Exception {
067
068 UploadServletRequest uploadServletRequest = null;
069
070 try {
071 String cmd = ParamUtil.getString(request, Constants.CMD);
072
073 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
074 WebKeys.THEME_DISPLAY);
075
076 long groupId = ParamUtil.getLong(request, "groupId");
077 String articleId = ParamUtil.getString(request, "articleId");
078 double version = ParamUtil.getDouble(
079 request, "version", JournalArticleConstants.VERSION_DEFAULT);
080
081 String languageId = LanguageUtil.getLanguageId(request);
082
083 String output = null;
084
085 if (cmd.equals(Constants.PREVIEW)) {
086 uploadServletRequest = PortalUtil.getUploadServletRequest(
087 request);
088
089 String title = ParamUtil.getString(
090 uploadServletRequest, "title");
091 String description = ParamUtil.getString(
092 uploadServletRequest, "description");
093 String type = ParamUtil.getString(uploadServletRequest, "type");
094 String structureId = ParamUtil.getString(
095 uploadServletRequest, "structureId");
096 String templateId = ParamUtil.getString(
097 uploadServletRequest, "templateId");
098
099 Date now = new Date();
100
101 Date createDate = now;
102 Date modifiedDate = now;
103 Date displayDate = now;
104
105 User user = PortalUtil.getUser(uploadServletRequest);
106
107 String xml = ParamUtil.getString(uploadServletRequest, "xml");
108
109 Document doc = SAXReaderUtil.read(xml);
110
111 Element root = doc.getRootElement();
112
113 String previewArticleId =
114 "PREVIEW_" +
115 PwdGenerator.getPassword(PwdGenerator.KEY3, 10);
116
117 format(
118 groupId, articleId, version, previewArticleId, root,
119 uploadServletRequest);
120
121 Map<String, String> tokens = JournalUtil.getTokens(
122 groupId, themeDisplay);
123
124 tokens.put("article_resource_pk", "-1");
125
126 JournalArticle article = new JournalArticleImpl();
127
128 article.setGroupId(groupId);
129 article.setCompanyId(user.getCompanyId());
130 article.setUserId(user.getUserId());
131 article.setUserName(user.getFullName());
132 article.setCreateDate(createDate);
133 article.setModifiedDate(modifiedDate);
134 article.setArticleId(articleId);
135 article.setVersion(version);
136 article.setTitle(title);
137 article.setDescription(description);
138 article.setContent(xml);
139 article.setType(type);
140 article.setStructureId(structureId);
141 article.setTemplateId(templateId);
142 article.setDisplayDate(displayDate);
143
144 output = JournalArticleLocalServiceUtil.getArticleContent(
145 article, templateId, null, languageId, themeDisplay);
146 }
147 else if (cmd.equals(Constants.VIEW)) {
148 JournalArticle article = JournalArticleServiceUtil.getArticle(
149 groupId, articleId, version);
150
151 output = JournalArticleLocalServiceUtil.getArticleContent(
152 article, article.getTemplateId(), null, languageId,
153 themeDisplay);
154 }
155 else {
156 output = JournalArticleServiceUtil.getArticleContent(
157 groupId, articleId, version, languageId, themeDisplay);
158 }
159
160 request.setAttribute(WebKeys.JOURNAL_ARTICLE_CONTENT, output);
161
162 if (output.startsWith("<?xml ")) {
163 return actionMapping.findForward(
164 "portlet.journal.raw_article_content");
165 }
166 else {
167 return actionMapping.findForward(
168 "portlet.journal.view_article_content");
169 }
170 }
171 catch (Exception e) {
172 PortalUtil.sendError(e, request, response);
173
174 return null;
175 }
176 finally {
177 if (uploadServletRequest != null) {
178 uploadServletRequest.cleanUp();
179 }
180 }
181 }
182
183 protected void format(
184 long groupId, String articleId, double version,
185 String previewArticleId, Element root,
186 UploadServletRequest uploadServletRequest)
187 throws Exception {
188
189 Iterator<Element> itr = root.elements().iterator();
190
191 while (itr.hasNext()) {
192 Element el = itr.next();
193
194 Element dynamicContent = el.element("dynamic-content");
195
196 String elInstanceId = el.attributeValue(
197 "instance-id", StringPool.BLANK);
198 String elName = el.attributeValue("name", StringPool.BLANK);
199 String elType = el.attributeValue("type", StringPool.BLANK);
200 String elContent = StringPool.BLANK;
201 String elLanguage = StringPool.BLANK;
202
203 if (dynamicContent != null) {
204 elContent = dynamicContent.getTextTrim();
205
206 elLanguage = dynamicContent.attributeValue(
207 "language-id", StringPool.BLANK);
208
209 if (!elLanguage.equals(StringPool.BLANK)) {
210 elLanguage = "_" + elLanguage;
211 }
212 }
213
214 if (elType.equals("image") && Validator.isNull(elContent)) {
215 File file = uploadServletRequest.getFile(
216 "structure_image_" + elName + elLanguage);
217 byte[] bytes = FileUtil.getBytes(file);
218
219 if ((bytes != null) && (bytes.length > 0)) {
220 long imageId =
221 JournalArticleImageLocalServiceUtil.getArticleImageId(
222 groupId, previewArticleId, version, elInstanceId,
223 elName, elLanguage, true);
224
225 String token = WebServerServletTokenUtil.getToken(imageId);
226
227 dynamicContent.setText(
228 "/image/journal/article?img_id=" + imageId + "&t=" +
229 token);
230
231 ImageLocalServiceUtil.updateImage(imageId, bytes);
232 }
233 else {
234 if (Validator.isNotNull(articleId)) {
235 long imageId =
236 JournalArticleImageLocalServiceUtil.
237 getArticleImageId(
238 groupId, articleId, version, elInstanceId,
239 elName, elLanguage);
240
241 String token = WebServerServletTokenUtil.getToken(
242 imageId);
243
244 dynamicContent.setText(
245 "/image/journal/article?img_id=" + imageId +
246 "&t=" + token);
247 }
248 }
249 }
250
251 format(
252 groupId, articleId, version, previewArticleId, el,
253 uploadServletRequest);
254 }
255 }
256
257 }