1   package com.trendmicro.grid.acl.metadata;
2   
3   import javax.xml.bind.annotation.*;
4   import java.util.*;
5   
6   import static com.trendmicro.grid.acl.metadata.Profile.isNull;
7   
8   /**
9    * Defines the entry describing a single meta element.
10   *
11   * @author Juergen_Kellerer, 2010-04-21
12   * @version 1.0
13   */
14  @XmlAccessorType(XmlAccessType.FIELD)
15  @XmlAccessorOrder(value = XmlAccessOrder.UNDEFINED)
16  @XmlType(name = "entry", namespace = ProfilesDocument.NS)
17  public class ProfileEntry {
18  
19  	@XmlAttribute(required = true)
20  	String metaName;
21  
22  	@XmlList
23  	@XmlAttribute(name = "validType", required = true)
24  	Set<DataType> validTypes = new LinkedHashSet<DataType>();
25  
26  	@XmlAttribute
27  	boolean readonly;
28  
29  	@XmlElement
30  	Set<String> requiredBy;
31  
32  	@XmlElement
33  	Set<String> providedBy;
34  
35  	@XmlElement(name = "requiredRole")
36  	Set<String> requiredRoles;
37  
38  	@XmlAttribute(required = true)
39  	AccessLevel accessLevel = AccessLevel.PRIVATE;
40  
41  	@XmlElement
42  	String description;
43  
44  	@XmlElement(name = "comment")
45  	String[] comments;
46  
47  	/**
48  	 * Creates a new profile entry when de-serializing from XML.
49  	 */
50  	public ProfileEntry() {
51  	}
52  
53  	/**
54  	 * Creates a new profile entry for the given Meta name.
55  	 *
56  	 * @param metaName   The name of the Meta element to create this entry on.
57  	 * @param validTypes A collection of valid data types for the Meta value.
58  	 */
59  	public ProfileEntry(String metaName, DataType... validTypes) {
60  		if (metaName == null || metaName.isEmpty() || isNull(validTypes))
61  			throw new IllegalArgumentException("Name and validTypes may not be left empty");
62  
63  		this.metaName = metaName;
64  		this.validTypes.addAll(Arrays.asList(validTypes));
65  	}
66  
67  	public String getMetaName() {
68  		return metaName;
69  	}
70  
71  	/**
72  	 * Is a builder-pattern method that sets the specified values and returns this instance.
73  	 *
74  	 * @param level		 the protection level to apply to the Meta element.
75  	 * @param requiredBy	the IDs of the required components.
76  	 * @param requiredRoles the roles required to access the element.
77  	 * @return This instance.
78  	 */
79  	public ProfileEntry values(AccessLevel level, String requiredBy, String... requiredRoles) {
80  		setAccessLevel(level);
81  		setRequiredBy(requiredBy);
82  		setRequiredRoles(requiredRoles);
83  
84  		return this;
85  	}
86  
87  	public boolean isReadonly() {
88  		return readonly;
89  	}
90  
91  	public void setReadonly(boolean readonly) {
92  		this.readonly = readonly;
93  	}
94  
95  	public String getDescription() {
96  		return description;
97  	}
98  
99  	public void setDescription(String description) {
100 		this.description = description;
101 	}
102 
103 	public List<String> getComments() {
104 		if (comments == null)
105 			return Collections.emptyList();
106 		return Arrays.asList(comments);
107 	}
108 
109 	public void setComments(String... comments) {
110 		this.comments = comments;
111 	}
112 
113 	public AccessLevel getAccessLevel() {
114 		return accessLevel;
115 	}
116 
117 	public void setAccessLevel(AccessLevel accessLevel) {
118 		this.accessLevel = accessLevel;
119 	}
120 
121 	public Set<String> getRequiredRoles() {
122 		return requiredRoles;
123 	}
124 
125 	public void setRequiredRoles(String... requiredRoles) {
126 		setRequiredRoles(isNull(requiredRoles) ? null : new LinkedHashSet<String>(Arrays.asList(requiredRoles)));
127 	}
128 
129 	public void setRequiredRoles(Set<String> requiredRoles) {
130 		this.requiredRoles = requiredRoles;
131 	}
132 
133 	public Set<DataType> getValidTypes() {
134 		return validTypes;
135 	}
136 
137 	public void setValidTypes(DataType... validTypes) {
138 		setValidTypes(isNull(validTypes) ? null : new LinkedHashSet<DataType>(Arrays.asList(validTypes)));
139 	}
140 
141 	public void setValidTypes(Set<DataType> validTypes) {
142 		if (validTypes == null)
143 			this.validTypes.clear();
144 		else
145 			this.validTypes = validTypes;
146 	}
147 
148 	public Set<String> getRequiredBy() {
149 		return requiredBy;
150 	}
151 
152 	public void setRequiredBy(String... requiredBy) {
153 		setRequiredBy(isNull(requiredBy) ? null : new LinkedHashSet<String>(Arrays.asList(requiredBy)));
154 	}
155 
156 	public void setRequiredBy(Set<String> requiredBy) {
157 		this.requiredBy = requiredBy;
158 	}
159 
160 	public Set<String> getProvidedBy() {
161 		return providedBy;
162 	}
163 
164 	public void setProvidedBy(String... providedBy) {
165 		setProvidedBy(isNull(providedBy) ? null : new LinkedHashSet<String>(Arrays.asList(providedBy)));
166 	}
167 
168 	public void setProvidedBy(Set<String> providedBy) {
169 		this.providedBy = providedBy;
170 	}
171 
172 	@Override
173 	public boolean equals(Object o) {
174 		if (this == o) return true;
175 		if (!(o instanceof ProfileEntry)) return false;
176 
177 		ProfileEntry that = (ProfileEntry) o;
178 
179 		if (description != null ? !description.equals(that.description) : that.description != null) return false;
180 		if (metaName != null ? !metaName.equals(that.metaName) : that.metaName != null) return false;
181 		if (accessLevel != that.accessLevel) return false;
182 		if (providedBy != null ? !providedBy.equals(that.providedBy) : that.providedBy != null) return false;
183 		if (requiredBy != null ? !requiredBy.equals(that.requiredBy) : that.requiredBy != null) return false;
184 		if (requiredRoles != null ? !requiredRoles.equals(that.requiredRoles) : that.requiredRoles != null)
185 			return false;
186 		if (validTypes != null ? !validTypes.equals(that.validTypes) : that.validTypes != null) return false;
187 
188 		return true;
189 	}
190 
191 	@Override
192 	public int hashCode() {
193 		int result = metaName != null ? metaName.hashCode() : 0;
194 		result = 31 * result + (requiredBy != null ? requiredBy.hashCode() : 0);
195 		result = 31 * result + (providedBy != null ? providedBy.hashCode() : 0);
196 		result = 31 * result + (validTypes != null ? validTypes.hashCode() : 0);
197 		result = 31 * result + (requiredRoles != null ? requiredRoles.hashCode() : 0);
198 		result = 31 * result + (accessLevel != null ? accessLevel.hashCode() : 0);
199 		result = 31 * result + (description != null ? description.hashCode() : 0);
200 		return result;
201 	}
202 
203 	@Override
204 	public String toString() {
205 		return "ProfileEntry{" +
206 			"metaName='" + metaName + '\'' +
207 			", requiredBy=" + (requiredBy == null ? null : Arrays.asList(requiredBy)) +
208 			", validTypes=" + (validTypes == null ? null : Arrays.asList(validTypes)) +
209 			", requiredRoles=" + (requiredRoles == null ? null : Arrays.asList(requiredRoles)) +
210 			", accessLevel=" + accessLevel +
211 			", description='" + description + '\'' +
212 			'}';
213 	}
214 }