View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.persistence.jpa.entity.group;
20  
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
23  import com.fasterxml.jackson.annotation.JsonInclude;
24  import com.fasterxml.jackson.annotation.JsonProperty;
25  import com.fasterxml.jackson.annotation.JsonSetter;
26  import java.util.ArrayList;
27  import java.util.List;
28  import org.apache.commons.lang3.builder.EqualsBuilder;
29  import org.apache.commons.lang3.builder.HashCodeBuilder;
30  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
31  import org.apache.syncope.core.persistence.api.entity.JSONPlainAttr;
32  import org.apache.syncope.core.persistence.api.entity.PlainAttrUniqueValue;
33  import org.apache.syncope.core.persistence.api.entity.PlainAttrValue;
34  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
35  import org.apache.syncope.core.persistence.api.entity.group.GPlainAttr;
36  import org.apache.syncope.core.persistence.api.entity.group.GPlainAttrValue;
37  import org.apache.syncope.core.persistence.api.entity.group.Group;
38  import org.apache.syncope.core.persistence.jpa.entity.AbstractPlainAttr;
39  import org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema;
40  import org.apache.syncope.core.spring.ApplicationContextProvider;
41  
42  @JsonIgnoreProperties("valuesAsStrings")
43  @JsonInclude(JsonInclude.Include.NON_EMPTY)
44  public class JPAJSONGPlainAttr extends AbstractPlainAttr<Group> implements GPlainAttr, JSONPlainAttr<Group> {
45  
46      private static final long serialVersionUID = 806271775349587902L;
47  
48      /**
49       * The owner of this attribute.
50       */
51      @JsonIgnore
52      private JPAJSONGroup owner;
53  
54      @JsonProperty
55      private String schema;
56  
57      /**
58       * Values of this attribute (if schema is not UNIQUE).
59       */
60      private final List<JPAJSONGPlainAttrValue> values = new ArrayList<>();
61  
62      /**
63       * Value of this attribute (if schema is UNIQUE).
64       */
65      @JsonProperty
66      private JPAJSONGPlainAttrUniqueValue uniqueValue;
67  
68      @Override
69      public Group getOwner() {
70          return owner;
71      }
72  
73      @Override
74      public void setOwner(final Group owner) {
75          checkType(owner, JPAJSONGroup.class);
76          this.owner = (JPAJSONGroup) owner;
77      }
78  
79      @JsonIgnore
80      @Override
81      public String getSchemaKey() {
82          return schema;
83      }
84  
85      @JsonIgnore
86      @Override
87      public JPAPlainSchema getSchema() {
88          return (JPAPlainSchema) ApplicationContextProvider.getBeanFactory().getBean(PlainSchemaDAO.class).find(schema);
89      }
90  
91      @Override
92      public void setSchema(final PlainSchema schema) {
93          if (schema != null) {
94              this.schema = schema.getKey();
95          }
96      }
97  
98      @JsonSetter("schema")
99      public void setSchema(final String schema) {
100         this.schema = schema;
101     }
102 
103     @Override
104     protected boolean addForMultiValue(final PlainAttrValue attrValue) {
105         checkType(attrValue, JPAJSONGPlainAttrValue.class);
106         return values.add((JPAJSONGPlainAttrValue) attrValue);
107     }
108 
109     @Override
110     public boolean add(final PlainAttrValue value) {
111         return addForMultiValue(value);
112     }
113 
114     @Override
115     public List<? extends GPlainAttrValue> getValues() {
116         return values;
117     }
118 
119     @JsonIgnore
120     public List<JPAJSONGPlainAttrValue> getPGValues() {
121         return values;
122     }
123 
124     @Override
125     public JPAJSONGPlainAttrUniqueValue getUniqueValue() {
126         return uniqueValue;
127     }
128 
129     @JsonIgnore
130     @Override
131     public void setUniqueValue(final PlainAttrUniqueValue uniqueValue) {
132         checkType(uniqueValue, JPAJSONGPlainAttrUniqueValue.class);
133         this.uniqueValue = (JPAJSONGPlainAttrUniqueValue) uniqueValue;
134     }
135 
136     @Override
137     public int hashCode() {
138         return new HashCodeBuilder().
139                 append(schema).
140                 append(values).
141                 append(uniqueValue).
142                 build();
143     }
144 
145     @Override
146     public boolean equals(final Object obj) {
147         if (this == obj) {
148             return true;
149         }
150         if (obj == null) {
151             return false;
152         }
153         if (getClass() != obj.getClass()) {
154             return false;
155         }
156         final JPAJSONGPlainAttr other = (JPAJSONGPlainAttr) obj;
157         return new EqualsBuilder().
158                 append(schema, other.schema).
159                 append(values, other.values).
160                 append(uniqueValue, other.uniqueValue).
161                 build();
162     }
163 }