001: package org.emforge.jbpm;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005:
006: import org.acegisecurity.userdetails.UserDetails;
007: import org.apache.commons.collections.Transformer;
008: import org.apache.commons.lang.StringUtils;
009: import org.emforge.projectmanager.ProjectService;
010: import org.emforge.projectmanager.base.MilestoneDO;
011: import org.emforge.projectmanager.base.ProjectDO;
012: import org.emforge.xfer.SelectValueTO;
013: import org.emforge.xfer.VariableTO;
014: import org.jbpm.context.def.VariableAccess;
015: import org.jbpm.graph.exe.ProcessInstance;
016:
017: import ru.emdev.EmForge.security.EmForgeUserDetails;
018: import ru.emdev.EmForge.security.UserFactory;
019: import ru.emdev.EmForge.security.dao.Role;
020:
021: /** Variable Transformer
022: * Used for converting VariableAccess and Value to VariableTO
023: *
024: * It is not implementing Transformer interface, since it is required 2 input objects
025: */
026: class VariableTransformer implements Transformer {
027: private ProjectService projectService;
028: private UserFactory userFactory;
029: private ProjectDO project;
030:
031: public VariableTransformer(ProjectService projectService,
032: UserFactory userFactory, ProcessInstance process,
033: String projectName) {
034: this .projectService = projectService;
035: this .userFactory = userFactory;
036:
037: // process may not to be specified - in this case we can get project by name
038: if (projectName != null) {
039: this .project = projectService.getProject(projectName);
040: } else {
041: // process should be specified!
042: this .project = ProcessTransformer.getProject(process);
043: }
044: }
045:
046: public VariableTO transform(Object i_input) {
047: VariableAccess var = (VariableAccess) i_input;
048: return transform(var, null);
049: }
050:
051: /** Create Step Variable from variable Access and Value
052: *
053: */
054: public VariableTO transform(VariableAccess i_var, Object i_value) {
055: VariableTO variableTO = transform(i_var.getMappedName(),
056: i_value);
057:
058: variableTO.setReadable(i_var.isReadable());
059: variableTO.setRequired(i_var.isRequired());
060: variableTO.setWritable(i_var.isWritable());
061:
062: return variableTO;
063: }
064:
065: public VariableTO transform(String i_label, Object i_value) {
066: String value = null;
067:
068: String displayValue = null;
069: SelectValueTO[] selectValues = null;
070:
071: // check variable type: is it milestone?
072: if (BpmVariable.MILESTONE.getVariable().equals(i_label)) {
073: // convert values from milestone
074: MilestoneDO milestone = (MilestoneDO) i_value;
075:
076: value = milestone != null ? milestone.getName().toString()
077: : null;
078: displayValue = milestone != null ? milestone
079: .getDisplayName() : null;
080:
081: } else if (isVarTypeUser(i_label)) {
082: value = i_value != null ? i_value.toString() : null;
083:
084: UserDetails user = null;
085: try {
086: user = i_value != null ? userFactory.getUser(value)
087: : null;
088: } catch (Exception ex) {
089: // just ignore it here
090: }
091: displayValue = user != null ? user.getUsername() : null;
092: } else {
093: value = i_value != null ? i_value.toString() : null;
094: displayValue = value;
095: }
096:
097: // create variable
098: VariableTO variableTO = new VariableTO();
099: variableTO.setLabel(i_label);
100: variableTO.setDisplayLabel(getDisplayLabel(i_label));
101:
102: variableTO.setValue(value);
103: variableTO.setDisplayValue(displayValue);
104:
105: // get select values for variable
106: selectValues = getVarSelectValues(i_label);
107:
108: variableTO.setSelectValues(selectValues);
109:
110: variableTO.setReadable(true);
111: variableTO.setRequired(true);
112: variableTO.setWritable(true);
113:
114: return variableTO;
115: }
116:
117: private boolean isVarTypeUser(String label) {
118: return label != null
119: && label.startsWith(BpmVariable.ASSIGNTO.getVariable());
120: }
121:
122: private String getDisplayLabel(String i_label) {
123: String label = StringUtils.removeStart(i_label, "_");
124:
125: if (label.startsWith(BpmVariable.ASSIGNTO.getVariable())) {
126: // This is Assign To Variable
127: if (label.length() > BpmVariable.ASSIGNTO.getVariable()
128: .length() + 1) {
129: String roleName = label.substring(BpmVariable.ASSIGNTO
130: .getVariable().length() + 1);
131: label = roleName;
132: } else {
133: label = "Assigned To";
134: }
135: }
136:
137: return label;
138: }
139:
140: protected SelectValueTO[] getVarSelectValues(String label) {
141: if (BpmVariable.MILESTONE.getVariable().equals(label)) {
142: SelectValueTO[] selectValues = null;
143:
144: if (project != null) {
145: Collection<MilestoneDO> milestones = projectService
146: .getOpenedMilestones(project);
147:
148: selectValues = new SelectValueTO[milestones.size() + 1];
149: selectValues[0] = new SelectValueTO("", "");
150:
151: int i = 1;
152: for (MilestoneDO openMilestone : milestones) {
153: selectValues[i++] = new SelectValueTO(openMilestone
154: .getName(), openMilestone.getDisplayName());
155: }
156: }
157:
158: return selectValues;
159: } else if (isVarTypeUser(label)) {
160: return getUserVarSelectValues(label);
161: } else {
162: return null;
163: }
164: }
165:
166: /** Created collection of selectValues for user-variable */
167: private SelectValueTO[] getUserVarSelectValues(String label) {
168:
169: Collection<SelectValueTO> result = new ArrayList<SelectValueTO>();
170:
171: // try to get role name
172: Role role = null;
173: if (Role.isVariableName(label)) {
174: String roleName = Role.getName(label);
175: role = userFactory.getRole(roleName);
176: if (role != null) {
177: result.add(new SelectValueTO(roleName, role
178: .getDecoratedName()));
179: } else {
180: result.add(new SelectValueTO("", ""));
181: }
182: } else {
183: result.add(new SelectValueTO("", ""));
184: }
185:
186: fillUsersCollection(project, role, result);
187: return result.toArray(new SelectValueTO[result.size()]);
188: }
189:
190: /**
191: * Fills specified collection with users of specified role
192: *
193: * @param role a role to get users
194: * @param result a collection to return
195: * @author szakusov, 21.02.2008: Implemented for request [^74641]
196: */
197: private void fillUsersCollection(ProjectDO project, Role role,
198: Collection<SelectValueTO> result) {
199:
200: Collection<EmForgeUserDetails> users = null;
201:
202: if (role != null) {
203: users = projectService.getUsers(project, role);
204: } else {
205: users = projectService.getAllProjectUsers(project);
206: }
207:
208: for (EmForgeUserDetails user : users) {
209: result.add(new SelectValueTO(user.getUsername(), user
210: .getDisplayName()));
211: }
212: }
213:
214: }
|