1 16 package com.blandware.atleap.webapp.action.news; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.NewsModuleConstants; 20 import com.blandware.atleap.common.parsers.html.HTMLPlainTextExtractor; 21 import com.blandware.atleap.common.util.ConvertUtil; 22 import com.blandware.atleap.model.news.NewsItem; 23 import com.blandware.atleap.search.SearchManager; 24 import com.blandware.atleap.service.exception.BeanAlreadyExistsException; 25 import com.blandware.atleap.service.news.NewsManager; 26 import com.blandware.atleap.webapp.action.core.BaseAction; 27 import com.blandware.atleap.webapp.form.NewsItemForm; 28 import com.blandware.atleap.webapp.util.core.*; 29 import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants; 30 import org.apache.commons.validator.GenericValidator; 31 import org.apache.struts.action.*; 32 import org.springframework.orm.ObjectOptimisticLockingFailureException; 33 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import java.io.ByteArrayInputStream ; 37 import java.io.InputStream ; 38 import java.util.*; 39 40 66 public final class UpdateNewsItemAction extends BaseAction { 67 76 public ActionForward execute(ActionMapping mapping, ActionForm form, 77 HttpServletRequest request, HttpServletResponse response) throws Exception { 78 79 NewsItemForm newsItemForm = (NewsItemForm) form; 80 String localesMode = newsItemForm.getLocalesMode(); 81 82 if ( isBackPressed(request) ) { 83 if (request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null) { 86 List contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales(); 87 request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales); 88 } 89 90 newsItemForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode); 92 93 return mapping.findForward("inputForward"); 94 } 95 96 if ( isCancelled(request) ) { 97 if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) { 98 String redirectUrl = (String ) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY); 99 ActionForward redirect = new ActionForward(redirectUrl, true); 100 return redirect; 101 } 102 return mapping.findForward("listNewsItems"); 103 } 104 105 if ( request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_SUBMITTED_ACTION_KEY) == null ) { 106 request.getSession().setAttribute(NewsModuleWebConstants.NEWS_ITEM_SUBMITTED_ACTION_KEY, "update"); 107 108 Long newsItemId = null; 109 if ( !GenericValidator.isBlankOrNull(newsItemForm.getId()) ) { 110 newsItemId = Long.valueOf(newsItemForm.getId()); 111 } else { 112 if ( log.isWarnEnabled() ) { 113 log.warn("Missing news item ID. Returning to list..."); 114 } 115 return mapping.findForward("listNewsItems"); 116 } 117 118 request.getSession().setAttribute(NewsModuleWebConstants.NEWS_ITEM_ID_KEY, newsItemId); 119 120 NewsManager newsManager = (NewsManager) getBean(NewsModuleConstants.NEWS_MANAGER_BEAN); 121 NewsItem newsItem = newsManager.retrieveNewsItem(newsItemId); 122 123 if ( newsItem == null ) { 124 ActionMessages errors = new ActionMessages(); 126 errors.add("newsItemNotFound", new ActionMessage("news.errors.notFound")); 127 saveErrors(request, errors); 128 return mapping.findForward("listNewsItems"); 129 } 130 131 Map newAnnotation = newsItem.getAnnotation(); 132 Map newBody = newsItem.getBody(); 133 134 WebappUtil.copyProperties(newsItem, newsItemForm, request); 135 136 Date publicationDate = newsItem.getPublicationDate(); 137 138 Date today = new Date(); 140 if ( publicationDate.compareTo(today) <= 0 ) { 141 newsItem.setActive(Boolean.TRUE); 142 } else { 143 newsItem.setActive(Boolean.FALSE); 144 } 145 146 if (!"current".equalsIgnoreCase(localesMode) && !"all".equalsIgnoreCase(localesMode)) { 148 localesMode = "selected"; 149 } 150 151 Map annotationMap = newsItemForm.getAnnotationMap(); 152 Map bodyMap = newsItemForm.getBodyMap(); 153 Map annotationCheckedBoxes = newsItemForm.getAnnotationCheckedBoxes(); 154 Map bodyCheckedBoxes = newsItemForm.getBodyCheckedBoxes(); 155 if ("selected".equalsIgnoreCase(localesMode)) { 156 for (Iterator i = annotationMap.entrySet().iterator(); i.hasNext();) { 157 Map.Entry entry = (Map.Entry) i.next(); 158 String localeIdentifier = (String ) entry.getKey(); 159 if (annotationCheckedBoxes.get(localeIdentifier) == null) { 160 annotationMap.put(localeIdentifier, newAnnotation.get(localeIdentifier)); 162 } 163 } 164 for (Iterator i = bodyMap.entrySet().iterator(); i.hasNext();) { 165 Map.Entry entry = (Map.Entry) i.next(); 166 String localeIdentifier = (String ) entry.getKey(); 167 if (bodyCheckedBoxes.get(localeIdentifier) == null) { 168 bodyMap.put(localeIdentifier, newBody.get(localeIdentifier)); 170 } 171 } 172 } 173 174 newsItem.setTitle(newsItemForm.getTitleMap()); 175 newsItem.setAnnotation(annotationMap); 176 newsItem.setBody(bodyMap); 177 String uri = NewsModuleWebConstants.NEWS_ITEM_URI_PREFIX + "/item" + newsItemForm.getId(); 178 newsItem.setUri(uri); 179 180 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 182 Set refs = new HashSet(); 183 for ( Iterator i = annotationMap.entrySet().iterator(); i.hasNext(); ) { 184 Map.Entry entry = (Map.Entry) i.next(); 185 String locale = (String ) entry.getKey(); 186 String annotation = (String ) entry.getValue(); 187 String body = (String ) bodyMap.get(locale); 188 InputStream annotationIS = new ByteArrayInputStream (ConvertUtil.convertToByteArray(annotation)); 189 refs.addAll(extractor.extractAllRefs(annotationIS, Constants.DEFAULT_ENCODING)); 190 if ( !GenericValidator.isBlankOrNull(body) ) { 191 InputStream bodyIS = new ByteArrayInputStream (ConvertUtil.convertToByteArray(body)); 192 refs.addAll(extractor.extractAllRefs(bodyIS, Constants.DEFAULT_ENCODING)); 193 } 194 } 195 196 Map linkedObjects = WebappUtil.mapObjectsToRefs(refs, request.getSession().getServletContext(), request.getContextPath()); 197 198 try { 199 newsManager.updateNewsItem(newsItem, linkedObjects); 200 201 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext()); 203 searchManager.unIndexPage(newsItem.getUri(), request); 204 if ( newsItem.getActive().booleanValue() ) { 205 searchManager.indexPage(newsItem, request); 206 } 207 208 CacheUtil cacheUtil = CacheUtil.getInstance(request); 209 cacheUtil.flushLocalizableFieldValueCache(newsItemId); 210 211 } catch ( BeanAlreadyExistsException e ) { 212 214 newsItemForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode); 216 217 ActionMessages errors = new ActionMessages(); 218 errors.add("newsItemAlreadyExists", new ActionMessage("errors.newsItemAlreadyExists", uri)); 219 saveErrors(request, errors); 220 return mapping.getInputForward(); 221 } catch ( ObjectOptimisticLockingFailureException e ) { 222 ActionMessages errors = new ActionMessages(); 224 errors.add("updateFailed", new ActionMessage("news.errors.updateFailed")); 225 saveErrors(request, errors); 226 227 Map oldAnnotation = annotationMap; 229 Map mergedAnnotation = new HashMap(); 230 for ( Iterator i = oldAnnotation.entrySet().iterator(); i.hasNext(); ) { 231 Map.Entry entry = (Map.Entry) i.next(); 232 String locale = (String ) entry.getKey(); 233 String oldValue = (String ) entry.getValue(); 234 String newValue = (String ) newAnnotation.get(locale); 235 String mergedValue = MergeUtil.mergeHtml(oldValue, newValue); 236 mergedAnnotation.put(locale, mergedValue); 237 } 238 request.getSession().setAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_ANNOTATION_KEY, mergedAnnotation); 239 240 Map oldBody = bodyMap; 242 Map mergedBody = new HashMap(); 243 for ( Iterator i = oldBody.entrySet().iterator(); i.hasNext(); ) { 244 Map.Entry entry = (Map.Entry) i.next(); 245 String locale = (String ) entry.getKey(); 246 String oldValue = (String ) entry.getValue(); 247 String newValue = (String ) newBody.get(locale); 248 String mergedValue = MergeUtil.mergeHtml(oldValue, newValue); 249 mergedBody.put(locale, mergedValue); 250 } 251 request.getSession().setAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_BODY_KEY, mergedBody); 252 253 newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode); 255 256 return mapping.findForward("callUpdateNewsItem"); 257 } 258 } 259 260 if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) { 261 String redirectUrl = (String ) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY); 262 ActionForward redirect = new ActionForward(redirectUrl, true); 263 return redirect; 264 } 265 266 return mapping.findForward("listNewsItems"); 267 } 268 } | Popular Tags |