KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > function > ErrorPopulator


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

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 /**
34  *
35  */

36 public abstract class ErrorPopulator extends InfoglueFunction
37 {
38     private final static Logger logger = Logger.getLogger(ErrorPopulator.class.getName());
39
40     /**
41      *
42      */

43     private static final String JavaDoc PACKAGE = "org.infoglue.cms.entities";
44     
45     /**
46      *
47      */

48     private StringManager stringManager;
49     
50     /**
51      *
52      */

53     protected ErrorPopulator()
54     {
55         super();
56     }
57     
58     /**
59      *
60      */

61     protected final void execute() throws WorkflowException
62     {
63         clean();
64         populate();
65     }
66     
67     /**
68      *
69      */

70     protected abstract void clean() throws WorkflowException;
71
72     /**
73      *
74      */

75     protected abstract void populate() throws WorkflowException;
76     
77     /**
78      *
79      */

80     protected final void clean(final String JavaDoc 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     /**
94      *
95      */

96     private void populateError(final ConstraintException e) throws WorkflowException
97     {
98         setPropertySetString(getErrorKey(e), getStringManager().getString(e.getErrorCode()));
99     }
100     
101     /**
102      *
103      */

104     private String JavaDoc getErrorKey(final ConstraintException e)
105     {
106         // The field name has the form:
107
// Content.<name>
108
// or
109
// ContentVersion.<name>
110
//
111
// convert this to:
112
// content_<name>
113
// or
114
// contentversion_<name>
115
// to better fit into the workflow framework.
116

117         final String JavaDoc fieldName = e.getFieldName();
118         final int index = fieldName.indexOf('.');
119         if(index == -1) // play it safe
120
{
121             return ERROR_PROPERTYSET_PREFIX + e.getFieldName();
122         }
123         final String JavaDoc before = fieldName.substring(0, index).toLowerCase();
124         final String JavaDoc after = fieldName.substring(index + 1);
125         final String JavaDoc 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     /**
132      * Method used for initializing the function; will be called before <code>execute</code> is called.
133      * <p><strong>Note</strong>! You must call <code>super.initialize()</code> first.</p>
134      *
135      * @throws WorkflowException if an error occurs during the initialization.
136      */

137     protected void initialize() throws WorkflowException
138     {
139         super.initialize();
140         stringManager = StringManagerFactory.getPresentationStringManager(PACKAGE, getLocale());
141     }
142     
143     /**
144      *
145      */

146     protected final StringManager getStringManager()
147     {
148         return stringManager;
149     }
150 }
151
Popular Tags