168f4ffb8ecbdabf2c87b357227dc80f074aa4b1
[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.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;
41
42 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
43 import org.springframework.stereotype.Service;
44 import org.springframework.validation.annotation.Validated;
45
46 import javax.ws.rs.core.Response;
47
48 @Service("activitySpecs")
49 @Validated
50 public class ActivitySpecsImpl implements ActivitySpecs {
51
52
53     private final ActivitySpecManager activitySpecManager =
54             new ActivitySpecManagerImpl(ItemManagerFactory.getInstance().createInterface(),
55                                                VersioningManagerFactory.getInstance().createInterface(),
56                                                new ActivitySpecDaoZusammenImpl(ZusammenAdaptorFactory.getInstance()
57                                                                                                 .createInterface()),
58                                                UniqueValueDaoFactory.getInstance().createInterface());
59
60     @Override
61     public Response createActivitySpec(ActivitySpecRequestDto request) {
62         ActivitySpecEntity activitySpec =
63                 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
64
65         activitySpec = activitySpecManager.createActivitySpec(activitySpec);
66         ActivitySpecCreateResponse createActivitySpecResponse = new MapActivitySpecToActivitySpecCreateResponse()
67                                                                         .applyMapping(activitySpec,
68                                                                                 ActivitySpecCreateResponse.class);
69
70         return Response.ok(createActivitySpecResponse).build();
71     }
72
73     @Override
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();
82     }
83
84     @Override
85     public Response updateActivitySpec(ActivitySpecRequestDto request, String activitySpecId, String versionId) {
86         ActivitySpecEntity activitySpec =
87                 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
88
89         activitySpec.setId(activitySpecId);
90         activitySpec.setVersion(new Version(versionId));
91
92         activitySpecManager.update(activitySpec);
93
94         return Response.ok(new InternalEmptyObject()).build();
95     }
96
97     @Override
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")
101                                                                     .build());
102         }
103         activitySpecManager.actOnAction(activitySpecId, versionId, request.getAction());
104         return Response.ok(new InternalEmptyObject()).build();
105     }
106
107     @Override
108     public Response list(String versionStatus) {
109
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)));
116
117         return Response.ok(results).build();
118     }
119 }