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.common.util.StringUtil; 21 import com.blandware.atleap.model.core.ContentField; 22 import com.blandware.atleap.model.core.ContentFieldValue; 23 import com.blandware.atleap.model.core.ContentLocale; 24 import com.blandware.atleap.service.core.ContentFieldManager; 25 import com.blandware.atleap.service.core.ContentLocaleManager; 26 import com.blandware.atleap.webapp.action.core.BaseAction; 27 import com.blandware.atleap.webapp.form.core.ContentFieldValueForm; 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.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionMessage; 35 import org.apache.struts.action.ActionMessages; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import java.util.ArrayList ; 40 import java.util.List ; 41 import java.util.SortedMap ; 42 import java.util.TreeMap ; 43 44 63 public final class ViewContentFieldValueAction extends BaseAction { 64 73 public ActionForward execute(ActionMapping mapping, ActionForm form, 74 HttpServletRequest request, HttpServletResponse response) throws Exception { 75 76 String redirectUrl = request.getParameter("redirectUrl"); 77 if (!GenericValidator.isBlankOrNull(redirectUrl)) { 78 request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl); 79 } 80 81 ContentFieldValueForm contentFieldValueForm = (ContentFieldValueForm) form; 82 Long contentFieldId = null; 83 if ( !GenericValidator.isBlankOrNull(contentFieldValueForm.getContentFieldId()) ) { 84 contentFieldId = Long.valueOf(contentFieldValueForm.getContentFieldId()); 85 } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY) != null ) { 86 contentFieldId = (Long ) request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY); 87 } else { 88 if ( log.isWarnEnabled() ) { 89 log.warn("Missing content field ID. Returning to index..."); 90 } 91 return mapping.findForward("admin"); 92 } 93 94 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId); 95 96 List cfvLocales = null; 97 98 if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY) != null ) { 99 cfvLocales = (List ) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY); 100 } else if ( !GenericValidator.isBlankOrNull(request.getParameter("locale")) ) { 101 String locale = request.getParameter("locale"); 102 cfvLocales = new ArrayList (); 103 cfvLocales.add(locale); 104 } else { 105 if ( log.isWarnEnabled() ) { 106 log.warn("Missing content field value locale. Returning to list..."); 107 } 108 return mapping.findForward("listContentFieldValues"); 109 } 110 111 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 112 ContentLocaleManager contentLocaleManager = (ContentLocaleManager) getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN); 113 114 ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId); 116 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_KEY, contentField); 117 118 SortedMap contentFieldValues = new TreeMap (); 119 120 SortedMap values = new TreeMap (); 122 123 for ( int i = 0; i < cfvLocales.size(); i++ ) { 124 String localeIdentifier = (String ) cfvLocales.get(i); 125 ContentLocale locale = contentLocaleManager.retrieveContentLocale(localeIdentifier); 126 if ( locale != null ) { 127 ContentFieldValue contentFieldValue = contentFieldManager.findContentFieldValueByContentFieldIdAndLocale(contentFieldId, localeIdentifier); 128 if ( contentFieldValue != null ) { 129 contentFieldValues.put(locale, contentFieldValue); 130 if ( contentFieldValue.getContentField().getType() == ContentField.LINE_TYPE || contentFieldValue.getContentField().getType() == ContentField.MULTILINE_TYPE ) { 131 String value = null; 132 if ( contentFieldValue.getContentField().getType() == ContentField.LINE_TYPE ) { 133 value = contentFieldValue.getSimpleValue(); 134 } else { 135 byte[] valueBytes = contentFieldValue.getValue(); 136 value = ConvertUtil.convertToString(valueBytes); 137 } 138 value = StringUtil.elToBr(StringUtil.htmlEncode(value)); 139 values.put(contentFieldValue.getId(), value); 140 } else { 141 byte[] value = contentFieldValue.getValue(); 142 values.put(contentFieldValue.getId(), ConvertUtil.convertToString(value)); 143 } 144 } 145 } 146 } 147 148 if ( contentFieldValues == null || contentFieldValues.isEmpty() ) { 149 ActionMessages errors = new ActionMessages(); 151 String localeString = ConvertUtil.convertListToString(cfvLocales, ", "); 152 errors.add("contentFieldValueNotFound", new ActionMessage("core.contentFieldValue.errors.notFound", localeString)); 153 saveErrors(request, errors); 154 return mapping.findForward("listContentFieldValues"); 155 } 156 157 request.setAttribute("contentFieldValues", contentFieldValues); 158 request.setAttribute(WebappConstants.CONTENT_FIELD_VALUE_KEY, values); 159 160 String ownerInfo = WebappUtil.getLocalizableInfo(contentField.getOwner(), request); 162 request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo); 163 164 return mapping.findForward("viewContentFieldValue"); 165 } 166 } | Popular Tags |