001/* 002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.ecm.platform.usermanager; 022 023import java.util.List; 024import java.util.Objects; 025 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.NuxeoGroup; 028import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel; 029 030/** 031 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a> 032 */ 033public class NuxeoGroupImpl implements NuxeoGroup { 034 035 private static final long serialVersionUID = -69828664399387083L; 036 037 protected DocumentModel model; 038 039 protected GroupConfig config = GroupConfig.DEFAULT; 040 041 public NuxeoGroupImpl(String name) { 042 this(name, name); 043 } 044 045 public NuxeoGroupImpl(String name, String label) { 046 if (name == null) { 047 throw new IllegalArgumentException("group name cannot be null"); 048 } 049 name = name.trim(); 050 label = label == null ? null : label.trim(); 051 052 model = SimpleDocumentModel.empty(); 053 model.setProperty(config.schemaName, config.idField, name); 054 model.setProperty(config.schemaName, config.labelField, label); 055 } 056 057 public NuxeoGroupImpl(DocumentModel model, GroupConfig config) { 058 this.model = model; 059 this.config = config; 060 } 061 062 @SuppressWarnings("unchecked") 063 @Override 064 public List<String> getMemberUsers() { 065 return (List<String>) model.getProperty(config.schemaName, config.membersField); 066 } 067 068 @SuppressWarnings("unchecked") 069 @Override 070 public List<String> getMemberGroups() { 071 return (List<String>) model.getProperty(config.schemaName, config.subGroupsField); 072 } 073 074 @SuppressWarnings("unchecked") 075 @Override 076 public List<String> getParentGroups() { 077 return (List<String>) model.getProperty(config.schemaName, config.parentGroupsField); 078 } 079 080 @Override 081 public void setMemberUsers(List<String> users) { 082 if (users == null) { 083 throw new IllegalArgumentException("member users list cannot be null"); 084 } 085 model.setProperty(config.schemaName, config.membersField, users); 086 } 087 088 @Override 089 public void setMemberGroups(List<String> groups) { 090 if (groups == null) { 091 throw new IllegalArgumentException("member groups list cannot be null"); 092 } 093 model.setProperty(config.schemaName, config.subGroupsField, groups); 094 } 095 096 @Override 097 public void setParentGroups(List<String> groups) { 098 if (groups == null) { 099 throw new IllegalArgumentException("parent groups list cannot be null"); 100 } 101 model.setProperty(config.schemaName, config.parentGroupsField, groups); 102 } 103 104 @Override 105 public String getName() { 106 return (String) model.getProperty(config.schemaName, config.idField); 107 } 108 109 @Override 110 public void setName(String name) { 111 model.setProperty(config.schemaName, config.idField, name); 112 } 113 114 @Override 115 public String getLabel() { 116 String label = (String) model.getProperty(config.schemaName, config.labelField); 117 return label == null ? getName() : label; 118 } 119 120 @Override 121 public void setLabel(String label) { 122 model.setProperty(config.schemaName, config.labelField, label); 123 } 124 125 @Override 126 public DocumentModel getModel() { 127 return model; 128 } 129 130 @Override 131 public boolean equals(Object other) { 132 if (other == this) { 133 return true; 134 } 135 if (other instanceof NuxeoGroupImpl) { 136 String name = getName(); 137 String otherName = ((NuxeoGroupImpl) other).getName(); 138 return Objects.equals(name, otherName); 139 } 140 return false; 141 } 142 143 @Override 144 public int hashCode() { 145 String name = getName(); 146 return name == null ? 0 : name.hashCode(); 147 } 148 149 @Override 150 public String toString() { 151 return getName(); 152 } 153 154}