2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.activityspec.api.rest.services;
19 import org.onap.sdc.activityspec.api.rest.ActivitySpecs;
20 import org.onap.sdc.activityspec.api.rest.mapping.MapActivitySpecRequestDtoToActivitySpecEntity;
21 import org.onap.sdc.activityspec.api.rest.mapping.MapActivitySpecToActivitySpecCreateResponse;
22 import org.onap.sdc.activityspec.api.rest.mapping.MapActivitySpecToActivitySpecGetResponse;
23 import org.onap.sdc.activityspec.api.rest.mapping.MapItemToListResponseDto;
24 import org.onap.sdc.activityspec.api.rest.types.ActivitySpecActionRequestDto;
25 import org.onap.sdc.activityspec.api.rest.types.ActivitySpecGetResponse;
26 import org.onap.sdc.activityspec.api.rest.types.ActivitySpecListResponseDto;
27 import org.onap.sdc.activityspec.api.rest.types.ActivitySpecRequestDto;
28 import org.onap.sdc.activityspec.api.rest.types.InternalEmptyObject;
29 import org.onap.sdc.activityspec.be.ActivitySpecManager;
30 import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity;
31 import org.onap.sdc.activityspec.be.impl.ActivitySpecManagerImpl;
32 import org.onap.sdc.activityspec.api.rest.types.ActivitySpecCreateResponse;
33 import org.onap.sdc.activityspec.be.dao.impl.ActivitySpecDaoZusammenImpl;
34 import org.openecomp.core.dao.UniqueValueDaoFactory;
35 import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory;
36 import org.openecomp.sdc.common.errors.CoreException;
37 import org.openecomp.sdc.common.errors.ErrorCode;
38 import org.openecomp.sdc.versioning.ItemManagerFactory;
39 import org.openecomp.sdc.versioning.VersioningManagerFactory;
40 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
43 import org.springframework.stereotype.Service;
44 import org.springframework.validation.annotation.Validated;
46 import javax.ws.rs.core.Response;
48 @Service("activitySpecs")
50 public class ActivitySpecsImpl implements ActivitySpecs {
53 private final ActivitySpecManager activitySpecManager =
54 new ActivitySpecManagerImpl(ItemManagerFactory.getInstance().createInterface(),
55 VersioningManagerFactory.getInstance().createInterface(),
56 new ActivitySpecDaoZusammenImpl(ZusammenAdaptorFactory.getInstance()
58 UniqueValueDaoFactory.getInstance().createInterface());
61 public Response createActivitySpec(ActivitySpecRequestDto request) {
62 ActivitySpecEntity activitySpec =
63 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
65 activitySpec = activitySpecManager.createActivitySpec(activitySpec);
66 ActivitySpecCreateResponse createActivitySpecResponse = new MapActivitySpecToActivitySpecCreateResponse()
67 .applyMapping(activitySpec,
68 ActivitySpecCreateResponse.class);
70 return Response.ok(createActivitySpecResponse).build();
74 public Response getActivitySpec(String activitySpecId, String versionId) {
75 ActivitySpecEntity activitySpec = new ActivitySpecEntity();
76 activitySpec.setId(activitySpecId);
77 activitySpec.setVersion(new Version(versionId));
78 final ActivitySpecEntity retrieved = activitySpecManager.get(activitySpec);
79 ActivitySpecGetResponse getResponse =
80 new MapActivitySpecToActivitySpecGetResponse().applyMapping(retrieved, ActivitySpecGetResponse.class);
81 return Response.ok(getResponse).build();
85 public Response updateActivitySpec(ActivitySpecRequestDto request, String activitySpecId, String versionId) {
86 ActivitySpecEntity activitySpec =
87 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
89 activitySpec.setId(activitySpecId);
90 activitySpec.setVersion(new Version(versionId));
92 activitySpecManager.update(activitySpec);
94 return Response.ok(new InternalEmptyObject()).build();
98 public Response actOnActivitySpec(ActivitySpecActionRequestDto request, String activitySpecId, String versionId) {
99 if (request.getAction() == null) {
100 throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage("Mandatory action field is missing")
103 activitySpecManager.actOnAction(activitySpecId, versionId, request.getAction());
104 return Response.ok(new InternalEmptyObject()).build();
108 public Response list(String versionStatus) {
110 GenericCollectionWrapper<ActivitySpecListResponseDto> results = new GenericCollectionWrapper<>();
111 MapItemToListResponseDto mapper = new MapItemToListResponseDto();
112 activitySpecManager.list(versionStatus).stream()
113 .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())).forEach(
114 activitySpecItem -> results.add(mapper.applyMapping(activitySpecItem,
115 ActivitySpecListResponseDto.class)));
117 return Response.ok(results).build();