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.user;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import javax.persistence.CascadeType;
24  import javax.persistence.Entity;
25  import javax.persistence.FetchType;
26  import javax.persistence.ManyToOne;
27  import javax.persistence.OneToMany;
28  import javax.persistence.OneToOne;
29  import javax.persistence.Table;
30  import javax.validation.Valid;
31  import org.apache.syncope.core.persistence.api.entity.PlainAttrUniqueValue;
32  import org.apache.syncope.core.persistence.api.entity.PlainAttrValue;
33  import org.apache.syncope.core.persistence.api.entity.user.UMembership;
34  import org.apache.syncope.core.persistence.api.entity.user.UPlainAttr;
35  import org.apache.syncope.core.persistence.api.entity.user.UPlainAttrUniqueValue;
36  import org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue;
37  import org.apache.syncope.core.persistence.api.entity.user.User;
38  import org.apache.syncope.core.persistence.jpa.entity.AbstractPlainAttr;
39  
40  @Entity
41  @Table(name = JPAUPlainAttr.TABLE)
42  public class JPAUPlainAttr extends AbstractPlainAttr<User> implements UPlainAttr {
43  
44      private static final long serialVersionUID = 6333601983691157406L;
45  
46      public static final String TABLE = "UPlainAttr";
47  
48      /**
49       * The owner of this attribute.
50       */
51      @ManyToOne(fetch = FetchType.EAGER)
52      private JPAUser owner;
53  
54      /**
55       * The membership of this attribute; might be {@code NULL} if this attribute is not related to a membership.
56       */
57      @ManyToOne(fetch = FetchType.EAGER)
58      private JPAUMembership membership;
59  
60      /**
61       * Values of this attribute (if schema is not UNIQUE).
62       */
63      @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, orphanRemoval = true, mappedBy = "attribute")
64      @Valid
65      private List<JPAUPlainAttrValue> values = new ArrayList<>();
66  
67      /**
68       * Value of this attribute (if schema is UNIQUE).
69       */
70      @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "attribute")
71      @Valid
72      private JPAUPlainAttrUniqueValue uniqueValue;
73  
74      @Override
75      public User getOwner() {
76          return owner;
77      }
78  
79      @Override
80      public void setOwner(final User owner) {
81          checkType(owner, JPAUser.class);
82          this.owner = (JPAUser) owner;
83      }
84  
85      @Override
86      public UMembership getMembership() {
87          return membership;
88      }
89  
90      @Override
91      public void setMembership(final UMembership membership) {
92          checkType(membership, JPAUMembership.class);
93          this.membership = (JPAUMembership) membership;
94      }
95  
96      @Override
97      protected boolean addForMultiValue(final PlainAttrValue attrValue) {
98          checkType(attrValue, JPAUPlainAttrValue.class);
99          return values.add((JPAUPlainAttrValue) attrValue);
100     }
101 
102     @Override
103     public List<? extends UPlainAttrValue> getValues() {
104         return values;
105     }
106 
107     @Override
108     public UPlainAttrUniqueValue getUniqueValue() {
109         return uniqueValue;
110     }
111 
112     @Override
113     public void setUniqueValue(final PlainAttrUniqueValue uniqueValue) {
114         checkType(uniqueValue, JPAUPlainAttrUniqueValue.class);
115         this.uniqueValue = (JPAUPlainAttrUniqueValue) uniqueValue;
116     }
117 }