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