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.am;
20  
21  import com.fasterxml.jackson.core.type.TypeReference;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Optional;
25  import javax.persistence.Entity;
26  import javax.persistence.EnumType;
27  import javax.persistence.Enumerated;
28  import javax.persistence.Lob;
29  import javax.persistence.PostLoad;
30  import javax.persistence.PostPersist;
31  import javax.persistence.PostUpdate;
32  import javax.persistence.PrePersist;
33  import javax.persistence.PreUpdate;
34  import javax.persistence.Table;
35  import javax.persistence.Transient;
36  import javax.validation.constraints.NotNull;
37  import org.apache.commons.lang3.StringUtils;
38  import org.apache.syncope.common.lib.auth.AuthModuleConf;
39  import org.apache.syncope.common.lib.to.Item;
40  import org.apache.syncope.common.lib.types.AuthModuleState;
41  import org.apache.syncope.core.persistence.api.entity.am.AuthModule;
42  import org.apache.syncope.core.persistence.jpa.entity.AbstractProvidedKeyEntity;
43  import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
44  
45  @Entity
46  @Table(name = JPAAuthModule.TABLE)
47  public class JPAAuthModule extends AbstractProvidedKeyEntity implements AuthModule {
48  
49      private static final long serialVersionUID = 5681033638234853077L;
50  
51      public static final String TABLE = "AuthModule";
52  
53      protected static final TypeReference<List<Item>> TYPEREF = new TypeReference<List<Item>>() {
54      };
55  
56      private String description;
57  
58      @Enumerated(EnumType.STRING)
59      @NotNull
60      private AuthModuleState authModuleState;
61  
62      @NotNull
63      private Integer authModuleOrder = 0;
64  
65      @Lob
66      private String items;
67  
68      @Transient
69      private final List<Item> itemList = new ArrayList<>();
70  
71      @Lob
72      private String jsonConf;
73  
74      @Override
75      public String getDescription() {
76          return description;
77      }
78  
79      @Override
80      public void setDescription(final String description) {
81          this.description = description;
82      }
83  
84      @Override
85      public AuthModuleState getState() {
86          return authModuleState;
87      }
88  
89      @Override
90      public void setState(final AuthModuleState state) {
91          this.authModuleState = state;
92      }
93  
94      @Override
95      public int getOrder() {
96          return Optional.ofNullable(authModuleOrder).orElse(0);
97      }
98  
99      @Override
100     public void setOrder(final int order) {
101         this.authModuleOrder = order;
102     }
103 
104     @Override
105     public List<Item> getItems() {
106         return itemList;
107     }
108 
109     @Override
110     public AuthModuleConf getConf() {
111         AuthModuleConf conf = null;
112         if (!StringUtils.isBlank(jsonConf)) {
113             conf = POJOHelper.deserialize(jsonConf, AuthModuleConf.class);
114         }
115 
116         return conf;
117     }
118 
119     @Override
120     public void setConf(final AuthModuleConf conf) {
121         jsonConf = POJOHelper.serialize(conf);
122     }
123 
124     protected void json2list(final boolean clearFirst) {
125         if (clearFirst) {
126             getItems().clear();
127         }
128         if (items != null) {
129             getItems().addAll(POJOHelper.deserialize(items, TYPEREF));
130         }
131     }
132 
133     @PostLoad
134     public void postLoad() {
135         json2list(false);
136     }
137 
138     @PostPersist
139     @PostUpdate
140     public void postSave() {
141         json2list(true);
142     }
143 
144     @PrePersist
145     @PreUpdate
146     public void list2json() {
147         items = POJOHelper.serialize(getItems());
148     }
149 
150 }