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;
20  
21  import com.fasterxml.jackson.core.type.TypeReference;
22  import java.util.HashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  import java.util.Optional;
26  import javax.persistence.Cacheable;
27  import javax.persistence.Entity;
28  import javax.persistence.Inheritance;
29  import javax.persistence.InheritanceType;
30  import javax.persistence.Lob;
31  import javax.persistence.PostLoad;
32  import javax.persistence.PostPersist;
33  import javax.persistence.PostUpdate;
34  import javax.persistence.PrePersist;
35  import javax.persistence.PreUpdate;
36  import javax.persistence.Table;
37  import javax.persistence.Transient;
38  import org.apache.syncope.core.persistence.api.entity.Schema;
39  import org.apache.syncope.core.persistence.jpa.validation.entity.SchemaKeyCheck;
40  import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
41  
42  @Entity
43  @Inheritance(strategy = InheritanceType.JOINED)
44  @Table(name = AbstractSchema.TABLE)
45  @Cacheable
46  @SchemaKeyCheck
47  public abstract class AbstractSchema extends AbstractProvidedKeyEntity implements Schema {
48  
49      private static final long serialVersionUID = -9222344997225831269L;
50  
51      public static final String TABLE = "SyncopeSchema";
52  
53      protected static final TypeReference<HashMap<Locale, String>> LABEL_TYPEREF =
54              new TypeReference<HashMap<Locale, String>>() {
55      };
56  
57      @Lob
58      private String labels;
59  
60      @Transient
61      private Map<Locale, String> labelMap = new HashMap<>();
62  
63      @Override
64      public Optional<String> getLabel(final Locale locale) {
65          return Optional.ofNullable(labelMap.get(locale));
66      }
67  
68      @Override
69      public Map<Locale, String> getLabels() {
70          return labelMap;
71      }
72  
73      protected void json2map(final boolean clearFirst) {
74          if (clearFirst) {
75              getLabels().clear();
76          }
77          if (labels != null) {
78              getLabels().putAll(POJOHelper.deserialize(labels, LABEL_TYPEREF));
79          }
80      }
81  
82      @PostLoad
83      public void postLoad() {
84          json2map(false);
85      }
86  
87      @PostPersist
88      @PostUpdate
89      public void postSave() {
90          json2map(true);
91      }
92  
93      @PrePersist
94      @PreUpdate
95      public void map2json() {
96          labels = POJOHelper.serialize(getLabels());
97      }
98  }