e0eec80fcb01e252fa65190a5851ed74ef70b527
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.activityspec.api.rest.services;
18
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.versioning.ItemManagerFactory;
37 import org.openecomp.sdc.versioning.VersioningManagerFactory;
38 import org.openecomp.sdc.versioning.dao.types.Version;
39
40 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
41 import org.springframework.stereotype.Service;
42 import org.springframework.validation.annotation.Validated;
43
44 import javax.ws.rs.core.Response;
45
46 @Service("activitySpecs")
47 @Validated
48 public class ActivitySpecsImpl implements ActivitySpecs {
49
50
51     private final ActivitySpecManager activitySpecManager =
52             new ActivitySpecManagerImpl(ItemManagerFactory.getInstance().createInterface(),
53                                                VersioningManagerFactory.getInstance().createInterface(),
54                                                new ActivitySpecDaoZusammenImpl(ZusammenAdaptorFactory.getInstance()
55                                                                                                 .createInterface()),
56                                                UniqueValueDaoFactory.getInstance().createInterface());
57
58     @Override
59     public Response createActivitySpec(ActivitySpecRequestDto request) {
60         ActivitySpecEntity activitySpec =
61                 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
62
63         activitySpec = activitySpecManager.createActivitySpec(activitySpec);
64         ActivitySpecCreateResponse createActivitySpecResponse = new MapActivitySpecToActivitySpecCreateResponse()
65                                                                         .applyMapping(activitySpec,
66                                                                                 ActivitySpecCreateResponse.class);
67
68         return Response.ok(createActivitySpecResponse).build();
69     }
70
71     @Override
72     public Response getActivitySpec(String activitySpecId, String versionId) {
73         ActivitySpecEntity activitySpec = new ActivitySpecEntity();
74         activitySpec.setId(activitySpecId);
75         activitySpec.setVersion(new Version(versionId));
76         final ActivitySpecEntity retrieved = activitySpecManager.get(activitySpec);
77         ActivitySpecGetResponse getResponse =
78                 new MapActivitySpecToActivitySpecGetResponse().applyMapping(retrieved, ActivitySpecGetResponse.class);
79         return Response.ok(getResponse).build();
80     }
81
82     @Override
83     public Response updateActivitySpec(ActivitySpecRequestDto request, String activitySpecId, String versionId) {
84         ActivitySpecEntity activitySpec =
85                 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
86
87         activitySpec.setId(activitySpecId);
88         activitySpec.setVersion(new Version(versionId));
89
90         activitySpecManager.update(activitySpec);
91
92         return Response.ok(new InternalEmptyObject()).build();
93     }
94
95     @Override
96     public Response actOnActivitySpec(ActivitySpecActionRequestDto request, String activitySpecId, String versionId) {
97         activitySpecManager.actOnAction(activitySpecId, versionId, request.getAction());
98         return Response.ok(new InternalEmptyObject()).build();
99     }
100
101     @Override
102     public Response list(String versionStatus) {
103
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(
109                         mapper.applyMapping(activitySpecItem, ActivitySpecListResponseDto.class)));
110
111         return Response.ok(results).build();
112     }
113
114 }