@Test public void testCreate() throws Exception { try (OperationContext ctx = new OperationContext(session)) { Map<String, Object> params = new HashMap<>(); params.put("groupname", GROUP); params.put("grouplabel", GROUPLABEL); params.put("members", new String[] { USER1 }); params.put("parentGroups", new String[] { GROUP2 }); automationService.run(ctx, CreateOrUpdateGroup.ID, params); NuxeoGroup group = userManager.getGroup(GROUP); assertNotNull(group); assertEquals(GROUPLABEL, group.getLabel()); assertEquals(Collections.singletonList(GROUP2), group.getParentGroups()); assertEquals(Collections.singletonList(USER1), group.getMemberUsers()); params.put("mode", "create"); try { automationService.run(ctx, CreateOrUpdateGroup.ID, params); } catch (OperationException e) { String msg = e.getMessage(); assertTrue(msg, msg.contains("Cannot create already-existing group: group")); } } }
@OperationMethod public void run() throws OperationException { String tenantGroupName = getTenantGroupName(groupName, tenantId); boolean create; DocumentModel groupDoc = userManager.getGroupModel(tenantGroupName); if (groupDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent group: " + groupName); } create = true; groupDoc = userManager.getBareGroupModel(); groupDoc.setProperty(GROUP_SCHEMA, GROUP_NAME, tenantGroupName); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing group: " + groupName); } create = false; checkCanCreateOrUpdateGroup(groupDoc); } if (members != null) { groupDoc.setProperty(GROUP_SCHEMA, MEMBERS, members); } if (subGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, SUB_GROUPS, subGroups); } if (parentGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, PARENT_GROUPS, parentGroups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(GROUP_TENANTID, tenantId), new SimpleEntry<>(GROUP_LABEL, groupLabel), new SimpleEntry<>(GROUP_DESCRIPTION, groupDescription))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(GROUP_COLON)) { key = key.substring(GROUP_COLON.length()); } groupDoc.setProperty(GROUP_SCHEMA, key, value); } checkCanCreateOrUpdateGroup(groupDoc); if (create) { userManager.createGroup(groupDoc); } else { userManager.updateGroup(groupDoc); } }
CreateOrUpdateGroup { @OperationMethod public void run() throws OperationException { String tenantGroupName = getTenantGroupName(groupName, tenantId); boolean create; DocumentModel groupDoc = userManager.getGroupModel(tenantGroupName); if (groupDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent group: " + groupName); } create = true; groupDoc = userManager.getBareGroupModel(); groupDoc.setProperty(GROUP_SCHEMA, GROUP_NAME, tenantGroupName); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing group: " + groupName); } create = false; checkCanCreateOrUpdateGroup(groupDoc); } if (members != null) { groupDoc.setProperty(GROUP_SCHEMA, MEMBERS, members); } if (subGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, SUB_GROUPS, subGroups); } if (parentGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, PARENT_GROUPS, parentGroups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(GROUP_TENANTID, tenantId), new SimpleEntry<>(GROUP_LABEL, groupLabel), new SimpleEntry<>(GROUP_DESCRIPTION, groupDescription))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(GROUP_COLON)) { key = key.substring(GROUP_COLON.length()); } groupDoc.setProperty(GROUP_SCHEMA, key, value); } checkCanCreateOrUpdateGroup(groupDoc); if (create) { userManager.createGroup(groupDoc); } else { userManager.updateGroup(groupDoc); } } }
CreateOrUpdateGroup { @OperationMethod public void run() throws OperationException { String tenantGroupName = getTenantGroupName(groupName, tenantId); boolean create; DocumentModel groupDoc = userManager.getGroupModel(tenantGroupName); if (groupDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent group: " + groupName); } create = true; groupDoc = userManager.getBareGroupModel(); groupDoc.setProperty(GROUP_SCHEMA, GROUP_NAME, tenantGroupName); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing group: " + groupName); } create = false; checkCanCreateOrUpdateGroup(groupDoc); } if (members != null) { groupDoc.setProperty(GROUP_SCHEMA, MEMBERS, members); } if (subGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, SUB_GROUPS, subGroups); } if (parentGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, PARENT_GROUPS, parentGroups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(GROUP_TENANTID, tenantId), new SimpleEntry<>(GROUP_LABEL, groupLabel), new SimpleEntry<>(GROUP_DESCRIPTION, groupDescription))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(GROUP_COLON)) { key = key.substring(GROUP_COLON.length()); } groupDoc.setProperty(GROUP_SCHEMA, key, value); } checkCanCreateOrUpdateGroup(groupDoc); if (create) { userManager.createGroup(groupDoc); } else { userManager.updateGroup(groupDoc); } }  }
CreateOrUpdateGroup { @OperationMethod public void run() throws OperationException { String tenantGroupName = getTenantGroupName(groupName, tenantId); boolean create; DocumentModel groupDoc = userManager.getGroupModel(tenantGroupName); if (groupDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent group: " + groupName); } create = true; groupDoc = userManager.getBareGroupModel(); groupDoc.setProperty(GROUP_SCHEMA, GROUP_NAME, tenantGroupName); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing group: " + groupName); } create = false; checkCanCreateOrUpdateGroup(groupDoc); } if (members != null) { groupDoc.setProperty(GROUP_SCHEMA, MEMBERS, members); } if (subGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, SUB_GROUPS, subGroups); } if (parentGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, PARENT_GROUPS, parentGroups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(GROUP_TENANTID, tenantId), new SimpleEntry<>(GROUP_LABEL, groupLabel), new SimpleEntry<>(GROUP_DESCRIPTION, groupDescription))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(GROUP_COLON)) { key = key.substring(GROUP_COLON.length()); } groupDoc.setProperty(GROUP_SCHEMA, key, value); } checkCanCreateOrUpdateGroup(groupDoc); if (create) { userManager.createGroup(groupDoc); } else { userManager.updateGroup(groupDoc); } }  @OperationMethod void run(); static String getTenantGroupName(String groupName, String tenantId); }
CreateOrUpdateGroup { @OperationMethod public void run() throws OperationException { String tenantGroupName = getTenantGroupName(groupName, tenantId); boolean create; DocumentModel groupDoc = userManager.getGroupModel(tenantGroupName); if (groupDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent group: " + groupName); } create = true; groupDoc = userManager.getBareGroupModel(); groupDoc.setProperty(GROUP_SCHEMA, GROUP_NAME, tenantGroupName); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing group: " + groupName); } create = false; checkCanCreateOrUpdateGroup(groupDoc); } if (members != null) { groupDoc.setProperty(GROUP_SCHEMA, MEMBERS, members); } if (subGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, SUB_GROUPS, subGroups); } if (parentGroups != null) { groupDoc.setProperty(GROUP_SCHEMA, PARENT_GROUPS, parentGroups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(GROUP_TENANTID, tenantId), new SimpleEntry<>(GROUP_LABEL, groupLabel), new SimpleEntry<>(GROUP_DESCRIPTION, groupDescription))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(GROUP_COLON)) { key = key.substring(GROUP_COLON.length()); } groupDoc.setProperty(GROUP_SCHEMA, key, value); } checkCanCreateOrUpdateGroup(groupDoc); if (create) { userManager.createGroup(groupDoc); } else { userManager.updateGroup(groupDoc); } }  @OperationMethod void run(); static String getTenantGroupName(String groupName, String tenantId); static final String ID; static final String CREATE_OR_UPDATE; static final String CREATE; static final String UPDATE; static final String GROUP_SCHEMA; static final String GROUP_NAME; static final String GROUP_LABEL; static final String GROUP_DESCRIPTION; static final String MEMBERS; static final String SUB_GROUPS; static final String PARENT_GROUPS; static final String GROUP_TENANTID; }