1 23 package org.infoglue.cms.applications.workflowtool.function; 24 25 import org.apache.log4j.Logger; 26 import org.infoglue.cms.exception.ConstraintException; 27 import org.infoglue.cms.util.ConstraintExceptionBuffer; 28 import org.infoglue.cms.util.StringManager; 29 import org.infoglue.cms.util.StringManagerFactory; 30 31 import com.opensymphony.workflow.WorkflowException; 32 33 36 public abstract class ErrorPopulator extends InfoglueFunction 37 { 38 private final static Logger logger = Logger.getLogger(ErrorPopulator.class.getName()); 39 40 43 private static final String PACKAGE = "org.infoglue.cms.entities"; 44 45 48 private StringManager stringManager; 49 50 53 protected ErrorPopulator() 54 { 55 super(); 56 } 57 58 61 protected final void execute() throws WorkflowException 62 { 63 clean(); 64 populate(); 65 } 66 67 70 protected abstract void clean() throws WorkflowException; 71 72 75 protected abstract void populate() throws WorkflowException; 76 77 80 protected final void clean(final String errorPrefix) throws WorkflowException 81 { 82 removeFromPropertySet(errorPrefix, true); 83 } 84 85 protected void populate(final ConstraintExceptionBuffer ceb) throws WorkflowException 86 { 87 for(ConstraintException e = ceb.toConstraintException(); e != null; e = e.getChainedException()) 88 { 89 populateError(e); 90 } 91 } 92 93 96 private void populateError(final ConstraintException e) throws WorkflowException 97 { 98 setPropertySetString(getErrorKey(e), getStringManager().getString(e.getErrorCode())); 99 } 100 101 104 private String getErrorKey(final ConstraintException e) 105 { 106 117 final String fieldName = e.getFieldName(); 118 final int index = fieldName.indexOf('.'); 119 if(index == -1) { 121 return ERROR_PROPERTYSET_PREFIX + e.getFieldName(); 122 } 123 final String before = fieldName.substring(0, index).toLowerCase(); 124 final String after = fieldName.substring(index + 1); 125 final String key = ERROR_PROPERTYSET_PREFIX + before + "_" + after; 126 logger.debug("error field name converted from [" + fieldName + "] to [" + before + "_" + after + "]."); 127 128 return key; 129 } 130 131 137 protected void initialize() throws WorkflowException 138 { 139 super.initialize(); 140 stringManager = StringManagerFactory.getPresentationStringManager(PACKAGE, getLocale()); 141 } 142 143 146 protected final StringManager getStringManager() 147 { 148 return stringManager; 149 } 150 } 151 | Popular Tags |