1 16 package com.blandware.atleap.webapp.action.core.contentFieldValue; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.util.ConvertUtil; 20 import com.blandware.atleap.model.core.ContentField; 21 import com.blandware.atleap.model.core.ContentFieldValue; 22 import com.blandware.atleap.model.core.ContentLocale; 23 import com.blandware.atleap.service.core.ContentFieldManager; 24 import com.blandware.atleap.service.core.ContentLocaleManager; 25 import com.blandware.atleap.webapp.action.core.BaseAction; 26 import com.blandware.atleap.webapp.form.core.ContentFieldValueForm; 27 import com.blandware.atleap.webapp.util.core.LocaleUtil; 28 import com.blandware.atleap.webapp.util.core.WebappConstants; 29 import com.blandware.atleap.webapp.util.core.WebappUtil; 30 import org.apache.commons.validator.GenericValidator; 31 import org.apache.struts.action.*; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import java.util.*; 36 37 59 public final class CallCreateOrUpdateContentFieldValueAction extends BaseAction { 60 69 public ActionForward execute(ActionMapping mapping, ActionForm form, 70 HttpServletRequest request, HttpServletResponse response) throws Exception { 71 72 75 String redirectUrl = request.getParameter("redirectUrl"); 76 if ( !GenericValidator.isBlankOrNull(redirectUrl) ) { 77 request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl); 79 } 80 81 request.getSession().removeAttribute(WebappConstants.CONTENT_FIELD_VALUE_SUBMITTED_ACTION_KEY); 82 83 ContentFieldValueForm contentFieldValueForm = (ContentFieldValueForm) form; 84 Long contentFieldId = null; 85 if ( !GenericValidator.isBlankOrNull(contentFieldValueForm.getContentFieldId()) ) { 86 contentFieldId = Long.valueOf(contentFieldValueForm.getContentFieldId()); 87 } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY) != null ) { 88 contentFieldId = (Long ) request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY); 89 } else { 90 if ( log.isWarnEnabled() ) { 91 log.warn("Missing content field ID. Returning to index..."); 92 } 93 return mapping.findForward("admin"); 94 } 95 96 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId); 97 98 if ( isCancelled(request) ) { 99 String url = (String ) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY); 101 if (!GenericValidator.isBlankOrNull(url)) { 102 request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY); 103 return new ActionForward(url, true); 104 } 105 return mapping.findForward("listContentFieldValues"); 106 } 107 108 if (!request.isUserInRole("core-contentFieldValue-createOrUpdate")) { 109 response.sendError(HttpServletResponse.SC_FORBIDDEN); 110 return null; 111 } 112 113 request.getSession().removeAttribute(WebappConstants.HTML_INPUT_TAG_ID_KEY); 115 116 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 117 ContentLocaleManager contentLocaleManager = (ContentLocaleManager) getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN); 118 119 List cfvLocales = null; 120 121 if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY) != null ) { 122 cfvLocales = (List) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY); 123 } else if ( !GenericValidator.isBlankOrNull(request.getParameter("locale")) ) { 124 String locale = request.getParameter("locale"); 125 cfvLocales = new ArrayList(); 126 cfvLocales.add(locale); 127 } 128 129 131 ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId); 132 133 if ( contentField == null ) { 134 ActionMessages errors = new ActionMessages(); 136 errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound")); 137 saveErrors(request, errors); 138 return mapping.findForward("listContentFields"); 139 } 140 141 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_KEY, contentField); 143 144 SortedMap contentFieldValues = new TreeMap(); 145 boolean CFVLocalesSpecified = cfvLocales != null && cfvLocales.size() > 0; 146 147 if ( !CFVLocalesSpecified ) { 148 List activeLocales = contentLocaleManager.getActiveLocales(); 149 Map contentFieldValuesMap = contentField.getContentFieldValuesMap(); 150 for (Iterator i = activeLocales.iterator(); i.hasNext();) { 151 ContentLocale contentLocale = (ContentLocale) i.next(); 152 ContentFieldValue contentFieldValue = (ContentFieldValue) contentFieldValuesMap.get(contentLocale.getIdentifier()); 153 contentFieldValues.put(contentLocale, contentFieldValue); 154 } 155 } else { 156 for ( int i = 0; i < cfvLocales.size(); i++ ) { 157 String localeIdentifier = (String ) cfvLocales.get(i); 158 ContentLocale locale = contentLocaleManager.retrieveContentLocale(localeIdentifier); 159 if ( locale != null ) { 160 ContentFieldValue contentFieldValue = contentFieldManager.findContentFieldValueByContentFieldIdAndLocale(contentFieldId, localeIdentifier); 161 contentFieldValues.put(locale, contentFieldValue); 162 } 163 } 164 165 } 166 167 if ( contentFieldValues == null || contentFieldValues.isEmpty() ) { 168 ActionMessages errors = new ActionMessages(); 170 errors.add("contentFieldValueNotFound", new ActionMessage("core.contentFieldValue.errors.nothingFound")); 171 saveErrors(request, errors); 172 return mapping.findForward("listContentFieldValues"); 173 } 174 175 Map mergedMap = null; 176 if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUES_MERGED_KEY) != null ) { 177 mergedMap = (Map) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUES_MERGED_KEY); 178 } 179 180 for ( Iterator i = contentFieldValues.entrySet().iterator(); i.hasNext(); ) { 181 Map.Entry entry = (Map.Entry) i.next(); 182 ContentLocale locale = (ContentLocale) entry.getKey(); 183 ContentFieldValue contentFieldValue = (ContentFieldValue) entry.getValue(); 184 String key = locale.getIdentifier(); 185 186 contentFieldValueForm.getLocaleMap().put(key, locale); 187 if ( contentFieldValue != null ) { 188 contentFieldValueForm.setId(key, contentFieldValue.getId()); 189 if ( mergedMap == null ) { 190 String newValue = null; 191 if ( contentFieldValue.getContentField().getType() == ContentField.LINE_TYPE ) { 192 newValue = contentFieldValue.getSimpleValue(); 193 } else { 194 newValue = ConvertUtil.convertToString(contentFieldValue.getValue()); 195 } 196 contentFieldValueForm.setValue(key, newValue); 197 } else { 198 contentFieldValueForm.setValue(key, mergedMap.get(contentFieldValue.getContentLocale().getIdentifier())); 199 } 200 contentFieldValueForm.setVersion(key, contentFieldValue.getVersion()); 201 } else { 202 contentFieldValueForm.setId(key, new Long (-1)); 203 contentFieldValueForm.setValue(key, new String ()); 204 contentFieldValueForm.setVersion(key, new Integer (0)); 205 } 206 } 207 208 if ( !CFVLocalesSpecified ) { 211 List contentLocales = new ArrayList(LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales()); 212 for ( Iterator i = contentLocales.iterator(); i.hasNext(); ) { 213 ContentLocale locale = (ContentLocale) i.next(); 214 String identifier = locale.getIdentifier(); 215 if ( !contentFieldValueForm.getIdMap().containsKey(identifier) ) { 216 contentFieldValueForm.setId(identifier, new Long (-1)); 217 contentFieldValueForm.setValue(identifier, new String ()); 218 contentFieldValueForm.setVersion(identifier, new Integer (0)); 219 contentFieldValueForm.getLocaleMap().put(identifier, locale); 223 } 224 } 225 } 226 227 String ownerInfo = WebappUtil.getLocalizableInfo(contentField.getOwner(), request); 229 request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo); 230 231 return mapping.findForward("createOrUpdateContentFieldValue"); 232 } 233 234 } | Popular Tags |