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.rest.cxf.service;
20  
21  import java.net.URI;
22  import java.time.OffsetDateTime;
23  import java.util.List;
24  import javax.ws.rs.BadRequestException;
25  import javax.ws.rs.core.Response;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.syncope.common.lib.form.SyncopeForm;
28  import org.apache.syncope.common.lib.to.ExecTO;
29  import org.apache.syncope.common.lib.to.PagedResult;
30  import org.apache.syncope.common.lib.to.SchedTaskTO;
31  import org.apache.syncope.common.lib.to.TaskTO;
32  import org.apache.syncope.common.lib.types.ExecStatus;
33  import org.apache.syncope.common.lib.types.TaskType;
34  import org.apache.syncope.common.rest.api.RESTHeaders;
35  import org.apache.syncope.common.rest.api.beans.ExecSpecs;
36  import org.apache.syncope.common.rest.api.beans.TaskQuery;
37  import org.apache.syncope.common.rest.api.service.TaskService;
38  import org.apache.syncope.core.logic.AbstractExecutableLogic;
39  import org.apache.syncope.core.logic.TaskLogic;
40  import org.springframework.stereotype.Service;
41  import org.springframework.util.CollectionUtils;
42  
43  @Service
44  public class TaskServiceImpl extends AbstractExecutableService implements TaskService {
45  
46      protected final TaskLogic logic;
47  
48      public TaskServiceImpl(final TaskLogic logic) {
49          this.logic = logic;
50      }
51  
52      @Override
53      protected AbstractExecutableLogic<?> getExecutableLogic() {
54          return logic;
55      }
56  
57      @Override
58      public Response create(final TaskType type, final SchedTaskTO taskTO) {
59          SchedTaskTO createdTask;
60          if (taskTO != null) {
61              createdTask = logic.createSchedTask(type, taskTO);
62          } else {
63              throw new BadRequestException();
64          }
65  
66          URI location = uriInfo.getAbsolutePathBuilder().path(createdTask.getKey()).build();
67          return Response.created(location).
68                  header(RESTHeaders.RESOURCE_KEY, createdTask.getKey()).
69                  build();
70      }
71  
72      @Override
73      public void delete(final TaskType type, final String key) {
74          logic.delete(type, key);
75      }
76  
77      @SuppressWarnings("unchecked")
78      @Override
79      public <T extends TaskTO> PagedResult<T> search(final TaskQuery query) {
80          Pair<Integer, List<T>> result = logic.search(
81                  query.getType(),
82                  query.getResource(),
83                  query.getNotification(),
84                  query.getAnyTypeKind(),
85                  query.getEntityKey(),
86                  query.getPage(),
87                  query.getSize(),
88                  getOrderByClauses(query.getOrderBy()),
89                  query.getDetails());
90          return buildPagedResult(result.getRight(), query.getPage(), query.getSize(), result.getLeft());
91      }
92  
93      @Override
94      public <T extends TaskTO> T read(final TaskType type, final String key, final boolean details) {
95          return logic.read(type, key, details);
96      }
97  
98      @Override
99      public void update(final TaskType type, final SchedTaskTO taskTO) {
100         logic.updateSchedTask(type, taskTO);
101     }
102 
103     @Override
104     public Response purgePropagations(
105             final OffsetDateTime since,
106             final List<ExecStatus> statuses,
107             final List<String> resources) {
108 
109         if (since == null && CollectionUtils.isEmpty(statuses) && CollectionUtils.isEmpty(resources)) {
110             return Response.status(Response.Status.PRECONDITION_FAILED).build();
111         }
112 
113         return Response.ok(logic.purgePropagations(since, statuses, resources)).build();
114     }
115 
116     @Override
117     public SyncopeForm getMacroTaskForm(final String key) {
118         return logic.getMacroTaskForm(key);
119     }
120 
121     @Override
122     public ExecTO execute(final ExecSpecs specs, final SyncopeForm macroTaskForm) {
123         return logic.execute(specs, macroTaskForm);
124     }
125 }