001: package ru.emdev.EmForge.security.dao;
002:
003: import java.io.Serializable;
004:
005: import org.apache.commons.lang.ObjectUtils;
006: import org.apache.commons.lang.StringUtils;
007: import org.emforge.base.PrimitiveImpl;
008: import org.emforge.jbpm.BpmSystemRole;
009: import org.emforge.jbpm.BpmVariable;
010: import org.emforge.xfer.EmForgeObject;
011:
012: /**
013: * Database-Based Role Implementation
014: */
015: public class Role extends PrimitiveImpl implements EmForgeObject,
016: Serializable {
017:
018: private static final long serialVersionUID = 4093049036841697921L;
019:
020: public static final Integer ROLE_TYPE_SITE = 0;
021: public static final Integer ROLE_TYPE_PROJECT = 1;
022: protected static final String ROLE_NAME_PREFIX = "Any ";
023: protected static final String ROLE_NAME_POSTFIX = "";
024: protected static final String ROLE_VARNAME_SEPARATOR = "_";
025:
026: public Integer roleType = ROLE_TYPE_SITE;
027:
028: /**
029: * @return The role name
030: */
031: public static String getName(String roleVariable) {
032:
033: String result = "";
034: if (isVariableName(roleVariable)) {
035: result = roleVariable.substring(BpmVariable.ASSIGNTO
036: .getVariable().length()
037: + ROLE_VARNAME_SEPARATOR.length());
038: }
039: return result;
040: }
041:
042: /**
043: * Checks if the specified name is "AssignTo" variable
044: *
045: * @return <code>True</code> if the specified name is "AssignTo" variable
046: */
047: public static boolean isVariableName(String i_name) {
048:
049: String name = ObjectUtils.toString(i_name);
050: return name.startsWith(BpmVariable.ASSIGNTO.getVariable()
051: + ROLE_VARNAME_SEPARATOR)
052: || BpmSystemRole.isSystemRole(name);
053: }
054:
055: /**
056: * Retrives role variable name by specified role name
057: *
058: * @return Role variable name
059: */
060: public static String getVariableName(String i_roleName) {
061:
062: String result = "";
063:
064: if (BpmSystemRole.isSystemRole(i_roleName)) {
065: result = BpmSystemRole.getVariableName(i_roleName);
066: } else if (StringUtils.isNotEmpty(i_roleName)) {
067: result = BpmVariable.ASSIGNTO.getVariable()
068: + ROLE_VARNAME_SEPARATOR + i_roleName;
069: }
070: return result;
071: }
072:
073: /**
074: * Returns name of role from assign_to variable name
075: *
076: * @param i_variableName
077: * @return role name
078: */
079: public static String getRoleNameFromVariableName(
080: String i_variableName) {
081:
082: if (!i_variableName.startsWith(BpmVariable.ASSIGNTO
083: .getVariable()
084: + ROLE_VARNAME_SEPARATOR)) {
085: return null;
086: }
087:
088: return i_variableName.substring((BpmVariable.ASSIGNTO
089: .getVariable() + ROLE_VARNAME_SEPARATOR).length());
090: }
091:
092: /**
093: * Retrives role variable name of this role
094: *
095: * @return Role variable name
096: */
097: public String getVariableName() {
098:
099: return getVariableName(getName());
100: }
101:
102: /**
103: * Adds prefix and postfix to display name of the role
104: *
105: * @return Decorated role name
106: */
107: public String getDecoratedName() {
108:
109: return ROLE_NAME_PREFIX + getDisplayName() + ROLE_NAME_POSTFIX;
110: }
111:
112: /**
113: * @return
114: */
115: public Integer getRoleType() {
116:
117: return roleType;
118: }
119:
120: /**
121: * @param roleType
122: */
123: public void setRoleType(Integer roleType) {
124:
125: this .roleType = roleType;
126: }
127:
128: /**
129: * @see java.lang.Object#equals(java.lang.Object)
130: */
131: @Override
132: public boolean equals(Object i_obj) {
133:
134: boolean result = false;
135:
136: if (i_obj != null) {
137: if (i_obj == this ) {
138: result = true;
139:
140: } else if (i_obj instanceof Role) {
141: if (((Role) i_obj).getId() == getId()) {
142: result = true;
143: }
144: }
145: }
146:
147: return result;
148: }
149: }
|