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.*;
25 import org.onap.sdc.activityspec.be.ActivitySpecManager;
26 import org.onap.sdc.activityspec.be.dao.impl.ActivitySpecDaoZusammenImpl;
27 import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity;
28 import org.onap.sdc.activityspec.be.impl.ActivitySpecManagerImpl;
29 import org.openecomp.core.dao.UniqueValueDaoFactory;
30 import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.openecomp.sdc.common.errors.ErrorCode;
33 import org.openecomp.sdc.versioning.ItemManagerFactory;
34 import org.openecomp.sdc.versioning.VersioningManagerFactory;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
37 import org.springframework.stereotype.Service;
38 import org.springframework.validation.annotation.Validated;
40 import javax.ws.rs.core.Response;
42 @Service("activitySpecs")
44 public class ActivitySpecsImpl implements ActivitySpecs {
47 private final ActivitySpecManager activitySpecManager =
48 new ActivitySpecManagerImpl(ItemManagerFactory.getInstance().createInterface(),
49 VersioningManagerFactory.getInstance().createInterface(),
50 new ActivitySpecDaoZusammenImpl(ZusammenAdaptorFactory.getInstance()
52 UniqueValueDaoFactory.getInstance().createInterface());
55 public Response createActivitySpec(ActivitySpecRequestDto request) {
56 ActivitySpecEntity activitySpec =
57 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
59 activitySpec = activitySpecManager.createActivitySpec(activitySpec);
60 ActivitySpecCreateResponse createActivitySpecResponse = new MapActivitySpecToActivitySpecCreateResponse()
61 .applyMapping(activitySpec,
62 ActivitySpecCreateResponse.class);
64 return Response.ok(createActivitySpecResponse).build();
68 public Response getActivitySpec(String activitySpecId, String versionId) {
69 ActivitySpecEntity activitySpec = new ActivitySpecEntity();
70 activitySpec.setId(activitySpecId);
71 activitySpec.setVersion(new Version(versionId));
72 final ActivitySpecEntity retrieved = activitySpecManager.get(activitySpec);
73 ActivitySpecGetResponse getResponse =
74 new MapActivitySpecToActivitySpecGetResponse().applyMapping(retrieved, ActivitySpecGetResponse.class);
75 return Response.ok(getResponse).build();
79 public Response updateActivitySpec(ActivitySpecRequestDto request, String activitySpecId, String versionId) {
80 ActivitySpecEntity activitySpec =
81 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
83 activitySpec.setId(activitySpecId);
84 activitySpec.setVersion(new Version(versionId));
86 activitySpecManager.update(activitySpec);
88 return Response.ok(new InternalEmptyObject()).build();
92 public Response actOnActivitySpec(ActivitySpecActionRequestDto request, String activitySpecId, String versionId) {
93 if (request.getAction() == null) {
94 throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage("Mandatory action field is missing")
97 activitySpecManager.actOnAction(activitySpecId, versionId, request.getAction());
98 return Response.ok(new InternalEmptyObject()).build();
102 public Response list(String versionStatus) {
104 GenericCollectionWrapper<ActivitySpecListResponseDto> results = new GenericCollectionWrapper<>();
105 MapItemToListResponseDto mapper = new MapItemToListResponseDto();
106 activitySpecManager.list(versionStatus).stream()
107 .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())).forEach(
108 activitySpecItem -> results.add(mapper.applyMapping(activitySpecItem,
109 ActivitySpecListResponseDto.class)));
111 return Response.ok(results).build();