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.anyobject;
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.AnyObjectDAO;
31  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
32  import org.apache.syncope.core.persistence.api.entity.JSONPlainAttr;
33  import org.apache.syncope.core.persistence.api.entity.PlainAttrUniqueValue;
34  import org.apache.syncope.core.persistence.api.entity.PlainAttrValue;
35  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
36  import org.apache.syncope.core.persistence.api.entity.anyobject.AMembership;
37  import org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttr;
38  import org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttrValue;
39  import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
40  import org.apache.syncope.core.persistence.jpa.entity.AbstractPlainAttr;
41  import org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema;
42  import org.apache.syncope.core.spring.ApplicationContextProvider;
43  
44  @JsonIgnoreProperties("valuesAsStrings")
45  @JsonInclude(JsonInclude.Include.NON_EMPTY)
46  public class JPAJSONAPlainAttr extends AbstractPlainAttr<AnyObject> implements APlainAttr, JSONPlainAttr<AnyObject> {
47  
48      private static final long serialVersionUID = 806271775349587902L;
49  
50      /**
51       * The owner of this attribute.
52       */
53      @JsonIgnore
54      private JPAJSONAnyObject owner;
55  
56      @JsonProperty
57      private String schema;
58  
59      /**
60       * The membership of this attribute; might be {@code NULL} if this attribute is not related to a membership.
61       */
62      @JsonProperty
63      private String membership;
64  
65      /**
66       * Values of this attribute (if schema is not UNIQUE).
67       */
68      private final List<JPAJSONAPlainAttrValue> values = new ArrayList<>();
69  
70      /**
71       * Value of this attribute (if schema is UNIQUE).
72       */
73      @JsonProperty
74      private JPAJSONAPlainAttrUniqueValue uniqueValue;
75  
76      @Override
77      public AnyObject getOwner() {
78          return owner;
79      }
80  
81      @Override
82      public void setOwner(final AnyObject owner) {
83          checkType(owner, JPAJSONAnyObject.class);
84          this.owner = (JPAJSONAnyObject) owner;
85      }
86  
87      @JsonIgnore
88      @Override
89      public String getSchemaKey() {
90          return schema;
91      }
92  
93      @JsonIgnore
94      @Override
95      public JPAPlainSchema getSchema() {
96          return (JPAPlainSchema) ApplicationContextProvider.getBeanFactory().getBean(PlainSchemaDAO.class).find(schema);
97      }
98  
99      @Override
100     public void setSchema(final PlainSchema schema) {
101         if (schema != null) {
102             this.schema = schema.getKey();
103         }
104     }
105 
106     @JsonSetter("schema")
107     public void setSchema(final String schema) {
108         this.schema = schema;
109     }
110 
111     @JsonIgnore
112     public String getMembershipKey() {
113         return membership;
114     }
115 
116     @JsonIgnore
117     @Override
118     public AMembership getMembership() {
119         return ApplicationContextProvider.getBeanFactory().getBean(AnyObjectDAO.class).findMembership(membership);
120     }
121 
122     @Override
123     public void setMembership(final AMembership membership) {
124         if (membership != null) {
125             this.membership = membership.getKey();
126         }
127     }
128 
129     @JsonSetter("membership")
130     public void setMembership(final String membership) {
131         this.membership = membership;
132     }
133 
134     @Override
135     protected boolean addForMultiValue(final PlainAttrValue attrValue) {
136         checkType(attrValue, JPAJSONAPlainAttrValue.class);
137         return values.add((JPAJSONAPlainAttrValue) attrValue);
138     }
139 
140     @Override
141     public boolean add(final PlainAttrValue value) {
142         return addForMultiValue(value);
143     }
144 
145     @Override
146     public List<? extends APlainAttrValue> getValues() {
147         return values;
148     }
149 
150     @JsonIgnore
151     public List<JPAJSONAPlainAttrValue> getPGValues() {
152         return values;
153     }
154 
155     @Override
156     public JPAJSONAPlainAttrUniqueValue getUniqueValue() {
157         return uniqueValue;
158     }
159 
160     @JsonIgnore
161     @Override
162     public void setUniqueValue(final PlainAttrUniqueValue uniqueValue) {
163         checkType(uniqueValue, JPAJSONAPlainAttrUniqueValue.class);
164         this.uniqueValue = (JPAJSONAPlainAttrUniqueValue) uniqueValue;
165     }
166 
167     @Override
168     public int hashCode() {
169         return new HashCodeBuilder().
170                 append(schema).
171                 append(membership).
172                 append(values).
173                 append(uniqueValue).
174                 build();
175     }
176 
177     @Override
178     public boolean equals(final Object obj) {
179         if (this == obj) {
180             return true;
181         }
182         if (obj == null) {
183             return false;
184         }
185         if (getClass() != obj.getClass()) {
186             return false;
187         }
188         final JPAJSONAPlainAttr other = (JPAJSONAPlainAttr) obj;
189         return new EqualsBuilder().
190                 append(schema, other.schema).
191                 append(membership, other.membership).
192                 append(values, other.values).
193                 append(uniqueValue, other.uniqueValue).
194                 build();
195     }
196 }