1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.persistence.jpa.entity;
20
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import javax.persistence.Column;
25 import javax.persistence.FetchType;
26 import javax.persistence.ManyToOne;
27 import javax.persistence.MappedSuperclass;
28 import javax.validation.constraints.NotNull;
29 import org.apache.syncope.core.persistence.api.attrvalue.validation.PlainAttrValidationManager;
30 import org.apache.syncope.core.persistence.api.entity.Any;
31 import org.apache.syncope.core.persistence.api.entity.AnyUtils;
32 import org.apache.syncope.core.persistence.api.entity.PlainAttr;
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.jpa.validation.entity.PlainAttrCheck;
37
38 @MappedSuperclass
39 @PlainAttrCheck
40 public abstract class AbstractPlainAttr<O extends Any<?>> extends AbstractGeneratedKeyEntity implements PlainAttr<O> {
41
42 private static final long serialVersionUID = -9115431608821806124L;
43
44 @NotNull
45 @ManyToOne(fetch = FetchType.EAGER)
46 @Column(name = "schema_id")
47 protected JPAPlainSchema schema;
48
49 @Override
50 public JPAPlainSchema getSchema() {
51 return schema;
52 }
53
54 @Override
55 public void setSchema(final PlainSchema schema) {
56 checkType(schema, JPAPlainSchema.class);
57 this.schema = (JPAPlainSchema) schema;
58 }
59
60 protected abstract boolean addForMultiValue(PlainAttrValue attrValue);
61
62 private void checkNonNullSchema() {
63 if (getSchema() == null) {
64 throw new IllegalStateException("First set owner then schema and finally add values");
65 }
66 }
67
68 @Override
69 public void add(final PlainAttrValidationManager validator, final String value, final PlainAttrValue attrValue) {
70 checkNonNullSchema();
71
72 attrValue.setAttr(this);
73 validator.validate(getSchema(), value, attrValue);
74
75 if (getSchema().isUniqueConstraint()) {
76 setUniqueValue((PlainAttrUniqueValue) attrValue);
77 } else {
78 if (!getSchema().isMultivalue()) {
79 getValues().clear();
80 }
81 addForMultiValue(attrValue);
82 }
83 }
84
85 @Override
86 public void add(final PlainAttrValidationManager validator, final String value, final AnyUtils anyUtils) {
87 checkNonNullSchema();
88
89 PlainAttrValue attrValue;
90 if (getSchema().isUniqueConstraint()) {
91 attrValue = anyUtils.newPlainAttrUniqueValue();
92 ((PlainAttrUniqueValue) attrValue).setSchema(getSchema());
93 } else {
94 attrValue = anyUtils.newPlainAttrValue();
95 }
96
97 add(validator, value, attrValue);
98 }
99
100 @Override
101 public List<String> getValuesAsStrings() {
102 List<String> result;
103 if (getUniqueValue() == null) {
104 result = getValues().stream().map(PlainAttrValue::getValueAsString).collect(Collectors.toList());
105 } else {
106 result = List.of(getUniqueValue().getValueAsString());
107 }
108
109 return Collections.unmodifiableList(result);
110 }
111
112 }