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 java.util.ArrayList;
22  import java.util.List;
23  import java.util.Optional;
24  import javax.persistence.CascadeType;
25  import javax.persistence.Entity;
26  import javax.persistence.EnumType;
27  import javax.persistence.Enumerated;
28  import javax.persistence.FetchType;
29  import javax.persistence.JoinColumn;
30  import javax.persistence.JoinTable;
31  import javax.persistence.ManyToMany;
32  import javax.persistence.ManyToOne;
33  import javax.persistence.OneToMany;
34  import javax.persistence.OneToOne;
35  import javax.persistence.Table;
36  import javax.persistence.UniqueConstraint;
37  import javax.validation.constraints.NotNull;
38  import org.apache.syncope.common.lib.types.IdMImplementationType;
39  import org.apache.syncope.common.lib.types.PullMode;
40  import org.apache.syncope.core.persistence.api.entity.Implementation;
41  import org.apache.syncope.core.persistence.api.entity.Realm;
42  import org.apache.syncope.core.persistence.api.entity.task.AnyTemplatePullTask;
43  import org.apache.syncope.core.persistence.api.entity.task.PullTask;
44  import org.apache.syncope.core.persistence.api.entity.task.SchedTask;
45  import org.apache.syncope.core.persistence.api.entity.task.TaskExec;
46  import org.apache.syncope.core.persistence.jpa.entity.JPAImplementation;
47  import org.apache.syncope.core.persistence.jpa.entity.JPARealm;
48  
49  @Entity
50  @Table(name = JPAPullTask.TABLE)
51  public class JPAPullTask extends AbstractProvisioningTask<PullTask> implements PullTask {
52  
53      private static final long serialVersionUID = -4141057723006682563L;
54  
55      public static final String TABLE = "PullTask";
56  
57      @Enumerated(EnumType.STRING)
58      @NotNull
59      private PullMode pullMode;
60  
61      @OneToOne
62      private JPAImplementation reconFilterBuilder;
63  
64      @ManyToOne(fetch = FetchType.EAGER, optional = false)
65      private JPARealm destinationRealm;
66  
67      @ManyToMany(fetch = FetchType.EAGER)
68      @JoinTable(name = "PullTaskAction",
69              joinColumns =
70              @JoinColumn(name = "task_id"),
71              inverseJoinColumns =
72              @JoinColumn(name = "implementation_id"),
73              uniqueConstraints =
74              @UniqueConstraint(columnNames = { "task_id", "implementation_id" }))
75      private List<JPAImplementation> actions = new ArrayList<>();
76  
77      @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "pullTask")
78      private List<JPAAnyTemplatePullTask> templates = new ArrayList<>();
79  
80      @OneToMany(targetEntity = JPAPullTaskExec.class,
81              cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "task")
82      private List<TaskExec<SchedTask>> executions = new ArrayList<>();
83  
84      @NotNull
85      private Boolean remediation = false;
86  
87      @Override
88      public PullMode getPullMode() {
89          return pullMode;
90      }
91  
92      @Override
93      public void setPullMode(final PullMode pullMode) {
94          this.pullMode = pullMode;
95      }
96  
97      @Override
98      public Implementation getReconFilterBuilder() {
99          return reconFilterBuilder;
100     }
101 
102     @Override
103     public void setReconFilterBuilder(final Implementation reconFilterBuilder) {
104         checkType(reconFilterBuilder, JPAImplementation.class);
105         checkImplementationType(reconFilterBuilder, IdMImplementationType.RECON_FILTER_BUILDER);
106         this.reconFilterBuilder = (JPAImplementation) reconFilterBuilder;
107     }
108 
109     @Override
110     public Realm getDestinationRealm() {
111         return destinationRealm;
112     }
113 
114     @Override
115     public void setDestinationRealm(final Realm destinationRealm) {
116         checkType(destinationRealm, JPARealm.class);
117         this.destinationRealm = (JPARealm) destinationRealm;
118     }
119 
120     @Override
121     public boolean add(final Implementation action) {
122         checkType(action, JPAImplementation.class);
123         checkImplementationType(action, IdMImplementationType.PULL_ACTIONS);
124         return actions.contains((JPAImplementation) action) || actions.add((JPAImplementation) action);
125     }
126 
127     @Override
128     public List<? extends Implementation> getActions() {
129         return actions;
130     }
131 
132     @Override
133     public boolean add(final AnyTemplatePullTask template) {
134         checkType(template, JPAAnyTemplatePullTask.class);
135         return this.templates.add((JPAAnyTemplatePullTask) template);
136     }
137 
138     @Override
139     public Optional<? extends AnyTemplatePullTask> getTemplate(final String anyType) {
140         return templates.stream().
141                 filter(template -> anyType != null && anyType.equals(template.getAnyType().getKey())).
142                 findFirst();
143     }
144 
145     @Override
146     public List<? extends AnyTemplatePullTask> getTemplates() {
147         return templates;
148     }
149 
150     @Override
151     public void setRemediation(final boolean remediation) {
152         this.remediation = remediation;
153     }
154 
155     @Override
156     public boolean isRemediation() {
157         return concurrentSettings != null ? true : remediation;
158     }
159 
160     @Override
161     protected Class<? extends TaskExec<SchedTask>> executionClass() {
162         return JPAPullTaskExec.class;
163     }
164 
165     @Override
166     protected List<TaskExec<SchedTask>> executions() {
167         return executions;
168     }
169 }