| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kew.xml; |
| 17 | |
|
| 18 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.ACTIVE; |
| 19 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.ATTRIBUTES; |
| 20 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.DESCRIPTION; |
| 21 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.GROUP; |
| 22 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.GROUPS; |
| 23 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.GROUP_ID; |
| 24 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.GROUP_NAME; |
| 25 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.GROUP_NAMESPACE; |
| 26 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.ID; |
| 27 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.KEY; |
| 28 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.MEMBERS; |
| 29 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.NAME; |
| 30 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.NAMESPACE; |
| 31 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.PRINCIPAL_ID; |
| 32 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.PRINCIPAL_NAME; |
| 33 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.TYPE; |
| 34 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.VALUE; |
| 35 | |
|
| 36 | |
import java.io.IOException; |
| 37 | |
import java.io.InputStream; |
| 38 | |
import java.util.ArrayList; |
| 39 | |
import java.util.HashMap; |
| 40 | |
import java.util.List; |
| 41 | |
|
| 42 | |
import javax.xml.parsers.ParserConfigurationException; |
| 43 | |
|
| 44 | |
import org.jdom.Document; |
| 45 | |
import org.jdom.Element; |
| 46 | |
import org.jdom.JDOMException; |
| 47 | |
import org.kuali.rice.core.util.XmlHelper; |
| 48 | |
import org.kuali.rice.core.xml.XmlException; |
| 49 | |
import org.kuali.rice.core.xml.dto.AttributeSet; |
| 50 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 51 | |
import org.kuali.rice.kew.util.Utilities; |
| 52 | |
import org.kuali.rice.kim.bo.Group; |
| 53 | |
import org.kuali.rice.kim.bo.entity.KimPrincipal; |
| 54 | |
import org.kuali.rice.kim.bo.group.dto.GroupInfo; |
| 55 | |
import org.kuali.rice.kim.bo.types.dto.KimTypeAttributeInfo; |
| 56 | |
import org.kuali.rice.kim.bo.types.dto.KimTypeInfo; |
| 57 | |
import org.kuali.rice.kim.service.IdentityManagementService; |
| 58 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
| 59 | |
import org.kuali.rice.kim.service.KIMServiceLocatorWeb; |
| 60 | |
import org.kuali.rice.kim.util.KimConstants; |
| 61 | |
import org.xml.sax.SAXException; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 0 | public class GroupXmlParser { |
| 73 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GroupXmlParser.class); |
| 74 | |
private static final boolean DEFAULT_ACTIVE_VALUE = true; |
| 75 | |
private static final String DEFAULT_GROUP_DESCRIPTION = ""; |
| 76 | 0 | private HashMap<String, List<String>> memberGroupIds = new HashMap<String, List<String>>(); |
| 77 | 0 | private HashMap<String, List<String>> memberGroupNames = new HashMap<String, List<String>>(); |
| 78 | 0 | private HashMap<String, List<String>> memberPrincipalIds = new HashMap<String, List<String>>(); |
| 79 | 0 | private AttributeSet groupAttributes = new AttributeSet(); |
| 80 | |
|
| 81 | |
public List<GroupInfo> parseGroups(InputStream input) throws IOException, XmlException { |
| 82 | |
try { |
| 83 | 0 | Document doc = XmlHelper.trimSAXXml(input); |
| 84 | 0 | Element root = doc.getRootElement(); |
| 85 | 0 | return parseGroups(root); |
| 86 | 0 | } catch (JDOMException e) { |
| 87 | 0 | throw new XmlException("Parse error.", e); |
| 88 | 0 | } catch (SAXException e){ |
| 89 | 0 | throw new XmlException("Parse error.",e); |
| 90 | 0 | } catch(ParserConfigurationException e){ |
| 91 | 0 | throw new XmlException("Parse error.",e); |
| 92 | |
} |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
@SuppressWarnings("unchecked") |
| 103 | |
public List<GroupInfo> parseGroups(Element element) throws XmlException { |
| 104 | 0 | List<GroupInfo> groupInfos = new ArrayList<GroupInfo>(); |
| 105 | 0 | for (Element groupsElement: (List<Element>) element.getChildren(GROUPS, GROUP_NAMESPACE)) { |
| 106 | |
|
| 107 | 0 | for (Element groupElement: (List<Element>) groupsElement.getChildren(GROUP, GROUP_NAMESPACE)) { |
| 108 | 0 | groupInfos.add(parseGroup(groupElement)); |
| 109 | |
} |
| 110 | |
} |
| 111 | 0 | for (GroupInfo groupInfo : groupInfos) { |
| 112 | 0 | IdentityManagementService identityManagementService = KIMServiceLocator.getIdentityManagementService(); |
| 113 | |
|
| 114 | |
|
| 115 | 0 | GroupInfo foundGroup = identityManagementService.getGroupByName(groupInfo.getNamespaceCode(), groupInfo.getGroupName()); |
| 116 | |
|
| 117 | 0 | if (foundGroup == null) { |
| 118 | 0 | if ( LOG.isInfoEnabled() ) { |
| 119 | 0 | LOG.info("Group named '" + groupInfo.getGroupName() + "' not found, creating new group named '" + groupInfo.getGroupName() + "'"); |
| 120 | |
} |
| 121 | |
try { |
| 122 | 0 | GroupInfo newGroupInfo = identityManagementService.createGroup(groupInfo); |
| 123 | |
|
| 124 | 0 | String key = newGroupInfo.getNamespaceCode().trim() + KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER + newGroupInfo.getGroupName().trim(); |
| 125 | 0 | addGroupMembers(newGroupInfo, key); |
| 126 | 0 | } catch (Exception e) { |
| 127 | 0 | throw new RuntimeException("Error creating group with name '" + groupInfo.getGroupName() + "'", e); |
| 128 | 0 | } |
| 129 | |
} else { |
| 130 | 0 | if ( LOG.isInfoEnabled() ) { |
| 131 | 0 | LOG.info("Group named '" + groupInfo.getGroupName() + "' found, creating a new version"); |
| 132 | |
} |
| 133 | |
try { |
| 134 | 0 | groupInfo.setGroupId(foundGroup.getGroupId()); |
| 135 | 0 | identityManagementService.updateGroup(foundGroup.getGroupId(), groupInfo); |
| 136 | |
|
| 137 | |
|
| 138 | 0 | identityManagementService.removeAllGroupMembers(foundGroup.getGroupId()); |
| 139 | |
|
| 140 | 0 | String key = groupInfo.getNamespaceCode().trim() + KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER + groupInfo.getGroupName().trim(); |
| 141 | 0 | addGroupMembers(groupInfo, key); |
| 142 | |
|
| 143 | 0 | } catch (Exception e) { |
| 144 | 0 | throw new RuntimeException("Error updating group.", e); |
| 145 | 0 | } |
| 146 | |
} |
| 147 | 0 | } |
| 148 | 0 | return groupInfos; |
| 149 | |
} |
| 150 | |
|
| 151 | |
@SuppressWarnings("unchecked") |
| 152 | |
private GroupInfo parseGroup(Element element) throws XmlException { |
| 153 | 0 | GroupInfo groupInfo = new GroupInfo(); |
| 154 | 0 | IdentityManagementService identityManagementService = KIMServiceLocator.getIdentityManagementService(); |
| 155 | 0 | groupInfo.setGroupName(element.getChildText(NAME, GROUP_NAMESPACE)); |
| 156 | |
|
| 157 | 0 | if (groupInfo.getGroupName() == null) { |
| 158 | 0 | throw new XmlException("Group must have a name."); |
| 159 | |
} |
| 160 | |
|
| 161 | 0 | String groupNamespace = element.getChildText(NAMESPACE, GROUP_NAMESPACE); |
| 162 | 0 | if (groupNamespace != null) { |
| 163 | 0 | groupInfo.setNamespaceCode(groupNamespace.trim()); |
| 164 | |
} else { |
| 165 | 0 | throw new XmlException("Namespace must have a value."); |
| 166 | |
} |
| 167 | |
|
| 168 | 0 | String id = element.getChildText(ID, GROUP_NAMESPACE); |
| 169 | 0 | if (id != null) { |
| 170 | 0 | groupInfo.setGroupId(id.trim()); |
| 171 | |
} else { |
| 172 | |
|
| 173 | |
} |
| 174 | |
|
| 175 | 0 | String description = element.getChildText(DESCRIPTION, GROUP_NAMESPACE); |
| 176 | 0 | if (description != null && !description.trim().equals("")) { |
| 177 | 0 | groupInfo.setGroupDescription(description); |
| 178 | |
} |
| 179 | |
|
| 180 | |
|
| 181 | 0 | String typeId = null; |
| 182 | 0 | List<KimTypeAttributeInfo> kimTypeAttributes = new ArrayList<KimTypeAttributeInfo>(); |
| 183 | 0 | if (element.getChild(TYPE, GROUP_NAMESPACE) != null) { |
| 184 | 0 | Element typeElement = element.getChild(TYPE, GROUP_NAMESPACE); |
| 185 | 0 | String typeNamespace = typeElement.getChildText(NAMESPACE, GROUP_NAMESPACE); |
| 186 | 0 | String typeName = typeElement.getChildText(NAME, GROUP_NAMESPACE); |
| 187 | 0 | KimTypeInfo kimTypeInfo = KIMServiceLocatorWeb.getTypeInfoService().getKimTypeByName(typeNamespace, typeName); |
| 188 | 0 | if (kimTypeInfo != null) { |
| 189 | 0 | typeId = kimTypeInfo.getKimTypeId(); |
| 190 | 0 | kimTypeAttributes = kimTypeInfo.getAttributeDefinitions(); |
| 191 | |
} else { |
| 192 | 0 | throw new XmlException("Invalid type name and namespace specified."); |
| 193 | |
} |
| 194 | 0 | } else { |
| 195 | 0 | KimTypeInfo kimTypeDefault = KIMServiceLocatorWeb.getTypeInfoService().getKimTypeByName(KimConstants.KIM_TYPE_DEFAULT_NAMESPACE, KimConstants.KIM_TYPE_DEFAULT_NAME); |
| 196 | 0 | if (kimTypeDefault != null) { |
| 197 | 0 | typeId = kimTypeDefault.getKimTypeId(); |
| 198 | 0 | kimTypeAttributes = kimTypeDefault.getAttributeDefinitions(); |
| 199 | |
} else { |
| 200 | 0 | throw new RuntimeException("Failed to locate the 'Default' group type! Please ensure that it's in your database."); |
| 201 | |
} |
| 202 | |
} |
| 203 | 0 | groupInfo.setKimTypeId(typeId); |
| 204 | |
|
| 205 | |
|
| 206 | 0 | groupInfo.setActive(DEFAULT_ACTIVE_VALUE); |
| 207 | 0 | if (element.getChildText(ACTIVE, GROUP_NAMESPACE) != null) { |
| 208 | 0 | String active = element.getChildText(ACTIVE, GROUP_NAMESPACE).trim(); |
| 209 | 0 | if (active.toUpperCase().equals("N") || active.toUpperCase().equals("FALSE")) { |
| 210 | 0 | groupInfo.setActive(false); |
| 211 | |
} |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | 0 | List<String> validAttributeKeys = new ArrayList<String>(); |
| 216 | 0 | for (KimTypeAttributeInfo attribute : kimTypeAttributes) { |
| 217 | 0 | validAttributeKeys.add(attribute.getAttributeName()); |
| 218 | |
} |
| 219 | |
|
| 220 | 0 | if (element.getChild(ATTRIBUTES, GROUP_NAMESPACE) != null) { |
| 221 | 0 | List<Element> attributes = element.getChild(ATTRIBUTES, GROUP_NAMESPACE).getChildren(); |
| 222 | 0 | AttributeSet attributeSet = new AttributeSet(); |
| 223 | 0 | for (Element attr : attributes ) { |
| 224 | 0 | String key = attr.getAttributeValue(KEY); |
| 225 | 0 | String value = attr.getAttributeValue(VALUE); |
| 226 | 0 | attributeSet.put(key, value); |
| 227 | 0 | if (!validAttributeKeys.contains(key)) { |
| 228 | 0 | throw new XmlException("Invalid attribute specified."); |
| 229 | |
} |
| 230 | 0 | } |
| 231 | 0 | if (attributeSet.size() > 0) { |
| 232 | 0 | groupInfo.setAttributes(attributeSet); |
| 233 | |
} |
| 234 | |
} |
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | 0 | List<Element> members = element.getChild(MEMBERS, GROUP_NAMESPACE).getChildren(); |
| 239 | 0 | for (Element member : members) { |
| 240 | 0 | String elementName = member.getName().trim(); |
| 241 | 0 | if (elementName.equals(PRINCIPAL_NAME)) { |
| 242 | 0 | String principalName = member.getText().trim(); |
| 243 | 0 | KimPrincipal principal = identityManagementService.getPrincipalByPrincipalName(principalName); |
| 244 | 0 | if (principal != null) { |
| 245 | 0 | addPrincipalToGroup(groupInfo.getNamespaceCode(), groupInfo.getGroupName(), principal.getPrincipalId()); |
| 246 | |
} else { |
| 247 | 0 | throw new XmlException("Principal Name "+principalName+" cannot be found."); |
| 248 | |
} |
| 249 | 0 | } else if (elementName.equals(PRINCIPAL_ID)) { |
| 250 | 0 | String xmlPrincipalId = member.getText().trim(); |
| 251 | 0 | KimPrincipal principal = identityManagementService.getPrincipal(xmlPrincipalId); |
| 252 | 0 | if (principal != null) { |
| 253 | 0 | addPrincipalToGroup(groupInfo.getNamespaceCode(), groupInfo.getGroupName(), principal.getPrincipalId()); |
| 254 | |
} else { |
| 255 | 0 | throw new XmlException("Principal Id "+xmlPrincipalId+" cannot be found."); |
| 256 | |
} |
| 257 | |
|
| 258 | 0 | } else if (elementName.equals(GROUP_ID)) { |
| 259 | 0 | String xmlGroupId = member.getText().trim(); |
| 260 | 0 | addGroupToGroup(groupInfo.getNamespaceCode(), groupInfo.getGroupName(), xmlGroupId); |
| 261 | 0 | } else if (elementName.equals(GROUP_NAME)) { |
| 262 | 0 | String xmlGroupName = member.getChildText(NAME, GROUP_NAMESPACE).trim(); |
| 263 | 0 | String xmlGroupNamespace = member.getChildText(NAMESPACE, GROUP_NAMESPACE).trim(); |
| 264 | 0 | addGroupNameToGroup(groupInfo.getNamespaceCode(), groupInfo.getGroupName(), xmlGroupNamespace, xmlGroupName); |
| 265 | 0 | } else { |
| 266 | 0 | LOG.error("Unknown member element: " + elementName); |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | 0 | } |
| 271 | |
|
| 272 | 0 | return groupInfo; |
| 273 | |
|
| 274 | |
} |
| 275 | |
|
| 276 | |
private void addPrincipalToGroup(String groupNamespace, String groupName, String principalId) { |
| 277 | 0 | String key = groupNamespace.trim() + KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER + groupName.trim(); |
| 278 | 0 | List<String> principalIds = memberPrincipalIds.get(key); |
| 279 | 0 | if (principalIds == null) { |
| 280 | 0 | principalIds = new ArrayList<String>(); |
| 281 | |
} |
| 282 | 0 | principalIds.add(principalId); |
| 283 | 0 | memberPrincipalIds.put(key, principalIds); |
| 284 | 0 | } |
| 285 | |
|
| 286 | |
private void addGroupToGroup(String groupNamespace, String groupName, String groupId) { |
| 287 | 0 | String key = groupNamespace.trim() + KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER + groupName.trim(); |
| 288 | 0 | List<String> groupIds = memberGroupIds.get(key); |
| 289 | 0 | if (groupIds == null) { |
| 290 | 0 | groupIds = new ArrayList<String>(); |
| 291 | |
} |
| 292 | 0 | groupIds.add(groupId); |
| 293 | 0 | memberGroupIds.put(key, groupIds); |
| 294 | 0 | } |
| 295 | |
|
| 296 | |
private void addGroupNameToGroup(String groupNamespace, String groupName, String memberGroupNamespace, String memberGroupName) { |
| 297 | 0 | String key = groupNamespace.trim() + KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER + groupName.trim(); |
| 298 | 0 | List<String> groupNames = memberGroupNames.get(key); |
| 299 | 0 | if (groupNames == null) { |
| 300 | 0 | groupNames = new ArrayList<String>(); |
| 301 | |
} |
| 302 | 0 | groupNames.add(memberGroupNamespace.trim() + KEWConstants.KIM_GROUP_NAMESPACE_NAME_DELIMITER_CHARACTER + memberGroupName.trim()); |
| 303 | 0 | memberGroupNames.put(key, groupNames); |
| 304 | 0 | } |
| 305 | |
|
| 306 | |
private void addGroupMembers(GroupInfo groupInfo, String key) throws XmlException { |
| 307 | 0 | IdentityManagementService identityManagementService = KIMServiceLocator.getIdentityManagementService(); |
| 308 | 0 | List<String> groupIds = memberGroupIds.get(key); |
| 309 | 0 | if (groupIds != null) { |
| 310 | 0 | for (String groupId : groupIds) { |
| 311 | 0 | Group group = identityManagementService.getGroup(groupId); |
| 312 | 0 | if (group != null) { |
| 313 | 0 | identityManagementService.addGroupToGroup(group.getGroupId(), groupInfo.getGroupId()); |
| 314 | |
} else { |
| 315 | 0 | throw new XmlException("Group Id "+groupId+" cannot be found."); |
| 316 | |
} |
| 317 | 0 | } |
| 318 | |
} |
| 319 | 0 | List<String> groupNames = memberGroupNames.get(key); |
| 320 | 0 | if (groupNames != null) { |
| 321 | 0 | for (String groupName : groupNames) { |
| 322 | 0 | Group group = identityManagementService.getGroupByName(Utilities.parseGroupNamespaceCode(groupName), Utilities.parseGroupName(groupName)); |
| 323 | 0 | if (group != null) { |
| 324 | 0 | identityManagementService.addGroupToGroup(group.getGroupId(), groupInfo.getGroupId()); |
| 325 | |
} else { |
| 326 | 0 | throw new XmlException("Group "+groupName+" cannot be found."); |
| 327 | |
} |
| 328 | 0 | } |
| 329 | |
} |
| 330 | 0 | List<String> principalIds = memberPrincipalIds.get(key); |
| 331 | 0 | if (principalIds != null) { |
| 332 | 0 | for (String principalId : principalIds) { |
| 333 | |
|
| 334 | 0 | identityManagementService.addPrincipalToGroup(principalId, groupInfo.getGroupId()); |
| 335 | |
} |
| 336 | |
} |
| 337 | |
|
| 338 | 0 | } |
| 339 | |
} |