1 16 package com.blandware.atleap.webapp.action.core.contentPage; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.model.core.ContentField; 20 import com.blandware.atleap.model.core.ContentFieldValue; 21 import com.blandware.atleap.model.core.ContentPage; 22 import com.blandware.atleap.model.core.Layout; 23 import com.blandware.atleap.search.SearchManager; 24 import com.blandware.atleap.service.core.ContentFieldManager; 25 import com.blandware.atleap.service.core.LayoutManager; 26 import com.blandware.atleap.service.core.PageManager; 27 import com.blandware.atleap.service.exception.BeanAlreadyExistsException; 28 import com.blandware.atleap.service.exception.OwnerNotFoundException; 29 import com.blandware.atleap.webapp.action.core.BaseAction; 30 import com.blandware.atleap.webapp.form.ContentPageForm; 31 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition; 32 import com.blandware.atleap.webapp.util.core.CacheUtil; 33 import com.blandware.atleap.webapp.util.core.WebappConstants; 34 import com.blandware.atleap.webapp.util.core.WebappUtil; 35 import org.apache.struts.action.ActionForm; 36 import org.apache.struts.action.ActionForward; 37 import org.apache.struts.action.ActionMapping; 38 import org.apache.struts.action.ActionMessage; 39 import org.apache.struts.action.ActionMessages; 40 import org.apache.struts.config.ModuleConfig; 41 import org.apache.struts.tiles.TilesUtil; 42 import org.apache.struts.util.ModuleUtils; 43 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.http.HttpServletResponse ; 46 import java.util.ArrayList ; 47 import java.util.Collection ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.LinkedList ; 51 import java.util.List ; 52 import java.util.Map ; 53 import java.util.Set ; 54 55 84 public final class CreateContentPageAction extends BaseAction { 85 94 public ActionForward execute(ActionMapping mapping, ActionForm form, 95 HttpServletRequest request, HttpServletResponse response) throws Exception { 96 97 if ( !isCancelled(request) ) { 98 ContentPageForm contentPageForm = (ContentPageForm) form; 99 100 if ( !WebappUtil.hasCorrectValues(contentPageForm.getTitleMap()) ) { 101 ActionMessages errors = new ActionMessages(); 103 errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "core.page.form.title"))); 104 saveErrors(request, errors, false); 105 saveToken(request); 106 return mapping.getInputForward(); 107 } 108 109 String uri = WebappConstants.CONTENT_PAGES_URI_PREFIX + contentPageForm.getUri(); 111 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, getServlet().getServletContext()); 112 String prefix = moduleConfig.getPrefix(); 113 if ( log.isDebugEnabled() ) { 114 log.debug("Prefix: " + prefix); 115 } 116 117 String actionPath = new String (); 118 actionPath = prefix + uri; 119 120 if ( log.isDebugEnabled() ) { 121 log.debug("Searching for action with path=" + actionPath); 122 } 123 124 if ( moduleConfig.findActionConfig(actionPath) != null ) { 125 if ( log.isWarnEnabled() ) { 127 log.warn("Action was found - returning input"); 128 } 129 ActionMessages errors = new ActionMessages(); 130 errors.add("contentPageIncorrectUri", new ActionMessage("core.contentPage.errors.incorrectUri")); 131 saveErrors(request, errors); 132 saveToken(request); 133 return mapping.getInputForward(); 134 } 135 136 ContentPage contentPage = new ContentPage(); 137 WebappUtil.copyProperties(contentPage, contentPageForm, request); 138 contentPage.setActive(Boolean.FALSE); 139 contentPage.setTitle(contentPageForm.getTitleMap()); 140 contentPage.setDescription(contentPageForm.getDescriptionMap()); 141 contentPage.setKeywords(contentPageForm.getKeywordsMap()); 142 contentPage.setUri(uri); 143 PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN); 144 Long layoutId = Long.valueOf(contentPageForm.getLayoutId()); 145 Long contentPageId = null; 146 try { 147 contentPageId = pageManager.createContentPage(contentPage, layoutId); 148 149 LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN); 151 Layout layout = layoutManager.retrieveLayout(layoutId); 152 153 if ( contentPage.getActive().booleanValue() ) { 154 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext()); 155 searchManager.reIndexPage(contentPage, request); 156 } 157 158 String overrideFields = contentPageForm.getOverrideFields(); 159 if ( !"none".equalsIgnoreCase(overrideFields) ) { 160 boolean all = "all".equalsIgnoreCase(overrideFields); 161 boolean empty = "empty".equalsIgnoreCase(overrideFields); 162 if ( all || empty ) { 163 164 LinkedList layouts = new LinkedList (); 166 layouts.addFirst(layout); 167 String definition = layout.getDefinition(); 168 while ( definition != null ) { 169 definition = ((HeritableComponentDefinition) TilesUtil.getDefinition(definition, request, servlet.getServletContext())).getExtends(); 170 if ( definition != null ) { 171 layout = layoutManager.findLayoutByDefinition(definition); 172 if ( layout != null ) { 173 layouts.addFirst(layout); 174 } 175 } 176 } 177 178 Map contentFields = new HashMap (); 180 for ( Iterator i = layouts.iterator(); i.hasNext(); ) { 181 layout = (Layout) i.next(); 182 contentFields.putAll(layout.getContentFieldsMap()); 183 } 184 185 contentPage = pageManager.retrieveContentPage(contentPageId); 187 188 Set pageFieldIdentifiers = contentPage.getContentFieldsMap().keySet(); 190 for ( Iterator i = pageFieldIdentifiers.iterator(); i.hasNext(); ) { 191 String fieldIdentifier = (String ) i.next(); 192 contentFields.remove(fieldIdentifier); 193 } 194 195 197 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 198 199 Collection layoutFields = contentFields.values(); 200 for ( Iterator i = layoutFields.iterator(); i.hasNext(); ) { 201 ContentField layoutField = (ContentField) i.next(); 202 boolean createField = all || layoutField.getContentFieldValues().isEmpty(); 203 if ( createField ) { 204 ContentField pageField = new ContentField(); 205 WebappUtil.copyProperties(pageField, layoutField, request); 206 pageField.setId(null); 207 pageField.setContentFieldValues(new ArrayList ()); 208 pageField.setInternal(Boolean.FALSE); 209 contentFieldManager.createContentField(pageField, contentPageId); 210 } 211 } 212 } 213 } 214 215 CacheUtil cacheUtil = CacheUtil.getInstance(request); 217 219 ContentField titleField = (ContentField) contentPage.getContentFieldsMap().get("title"); 221 List titleValues = titleField.getContentFieldValues(); 222 for ( Iterator i = titleValues.iterator(); i.hasNext(); ) { 223 ContentFieldValue fieldValue = (ContentFieldValue) i.next(); 224 String content = fieldValue.getSimpleValue(); 226 CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, titleField.getId(), titleField.getType(), fieldValue.getId()); 227 cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), titleField.getIdentifier(), fieldValue.getContentLocale().getIdentifier()); 228 } 229 230 ContentField descriptionField = (ContentField) contentPage.getContentFieldsMap().get("description"); 232 List descriptionValues = descriptionField.getContentFieldValues(); 233 for ( Iterator i = descriptionValues.iterator(); i.hasNext(); ) { 234 ContentFieldValue fieldValue = (ContentFieldValue) i.next(); 235 String content = fieldValue.getSimpleValue(); 237 CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, descriptionField.getId(), descriptionField.getType(), fieldValue.getId()); 238 cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), descriptionField.getIdentifier(), fieldValue.getContentLocale().getIdentifier()); 239 } 240 241 ContentField keywordsField = (ContentField) contentPage.getContentFieldsMap().get("keywords"); 243 List keywordsValues = keywordsField.getContentFieldValues(); 244 for ( Iterator i = keywordsValues.iterator(); i.hasNext(); ) { 245 ContentFieldValue fieldValue = (ContentFieldValue) i.next(); 246 String content = fieldValue.getSimpleValue(); 248 CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, keywordsField.getId(), keywordsField.getType(), fieldValue.getId()); 249 cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), keywordsField.getIdentifier(), fieldValue.getContentLocale().getIdentifier()); 250 } 251 252 } catch ( OwnerNotFoundException e ) { 253 ActionMessages errors = new ActionMessages(); 255 errors.add("ownerNotFound", new ActionMessage("core.contentPage.errors.ownerNotFound")); 256 saveErrors(request, errors); 257 return mapping.findForward("listContentPages"); 258 } catch ( BeanAlreadyExistsException e ) { 259 ActionMessages errors = new ActionMessages(); 261 errors.add("contentPageAlreadyExists", new ActionMessage("core.contentPage.errors.alreadyExists")); 262 saveErrors(request, errors); 263 saveToken(request); 264 return mapping.getInputForward(); 265 } 266 request.getSession().setAttribute(WebappConstants.CONTENT_PAGE_ID_KEY, contentPageId); 267 return mapping.findForward("viewContentPage"); 268 } else { 269 return mapping.findForward("listContentPages"); 270 } 271 } 272 } | Popular Tags |