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.task;
20  
21  import com.fasterxml.jackson.core.type.TypeReference;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Optional;
27  import javax.persistence.CascadeType;
28  import javax.persistence.Entity;
29  import javax.persistence.FetchType;
30  import javax.persistence.JoinColumn;
31  import javax.persistence.JoinTable;
32  import javax.persistence.Lob;
33  import javax.persistence.ManyToMany;
34  import javax.persistence.ManyToOne;
35  import javax.persistence.OneToMany;
36  import javax.persistence.PostLoad;
37  import javax.persistence.PostPersist;
38  import javax.persistence.PostUpdate;
39  import javax.persistence.PrePersist;
40  import javax.persistence.PreUpdate;
41  import javax.persistence.Table;
42  import javax.persistence.Transient;
43  import javax.persistence.UniqueConstraint;
44  import org.apache.syncope.common.lib.types.IdMImplementationType;
45  import org.apache.syncope.core.persistence.api.entity.Implementation;
46  import org.apache.syncope.core.persistence.api.entity.Realm;
47  import org.apache.syncope.core.persistence.api.entity.task.PushTask;
48  import org.apache.syncope.core.persistence.api.entity.task.SchedTask;
49  import org.apache.syncope.core.persistence.api.entity.task.TaskExec;
50  import org.apache.syncope.core.persistence.jpa.entity.JPAImplementation;
51  import org.apache.syncope.core.persistence.jpa.entity.JPARealm;
52  import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
53  
54  @Entity
55  @Table(name = JPAPushTask.TABLE)
56  public class JPAPushTask extends AbstractProvisioningTask<PushTask> implements PushTask {
57  
58      private static final long serialVersionUID = -4141057723006682564L;
59  
60      public static final String TABLE = "PushTask";
61  
62      protected static final TypeReference<HashMap<String, String>> FILTER_TYPEREF =
63              new TypeReference<HashMap<String, String>>() {
64      };
65  
66      @ManyToOne(fetch = FetchType.EAGER, optional = false)
67      private JPARealm sourceRealm;
68  
69      @Lob
70      private String filters;
71  
72      @Transient
73      private Map<String, String> filterMap = new HashMap<>();
74  
75      @ManyToMany(fetch = FetchType.EAGER)
76      @JoinTable(name = "PushTaskAction",
77              joinColumns =
78              @JoinColumn(name = "task_id"),
79              inverseJoinColumns =
80              @JoinColumn(name = "implementation_id"),
81              uniqueConstraints =
82              @UniqueConstraint(columnNames = { "task_id", "implementation_id" }))
83      private List<JPAImplementation> actions = new ArrayList<>();
84  
85      @OneToMany(targetEntity = JPAPushTaskExec.class,
86              cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "task")
87      private List<TaskExec<SchedTask>> executions = new ArrayList<>();
88  
89      @Override
90      public JPARealm getSourceRealm() {
91          return sourceRealm;
92      }
93  
94      @Override
95      public void setSourceRealm(final Realm sourceRealm) {
96          checkType(sourceRealm, JPARealm.class);
97          this.sourceRealm = (JPARealm) sourceRealm;
98      }
99  
100     @Override
101     public boolean add(final Implementation action) {
102         checkType(action, JPAImplementation.class);
103         checkImplementationType(action, IdMImplementationType.PUSH_ACTIONS);
104         return actions.contains((JPAImplementation) action) || actions.add((JPAImplementation) action);
105     }
106 
107     @Override
108     public List<? extends Implementation> getActions() {
109         return actions;
110     }
111 
112     @Override
113     public Optional<String> getFilter(final String anyType) {
114         return Optional.ofNullable(filterMap.get(anyType));
115     }
116 
117     @Override
118     public Map<String, String> getFilters() {
119         return filterMap;
120     }
121 
122     @Override
123     protected Class<? extends TaskExec<SchedTask>> executionClass() {
124         return JPAPushTaskExec.class;
125     }
126 
127     @Override
128     protected List<TaskExec<SchedTask>> executions() {
129         return executions;
130     }
131 
132     protected void json2map(final boolean clearFirst) {
133         if (clearFirst) {
134             getFilters().clear();
135         }
136         if (filters != null) {
137             getFilters().putAll(POJOHelper.deserialize(filters, FILTER_TYPEREF));
138         }
139     }
140 
141     @PostLoad
142     public void postLoad() {
143         json2map(false);
144     }
145 
146     @PostPersist
147     @PostUpdate
148     public void postSave() {
149         json2map(true);
150     }
151 
152     @PrePersist
153     @PreUpdate
154     public void map2json() {
155         filters = POJOHelper.serialize(getFilters());
156     }
157 }