re base code
[sdc.git] / services / activity-spec / activity-spec-web / activity-spec-service / src / main / java / org / onap / sdc / activityspec / api / rest / services / ActivitySpecsImpl.java
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.*;
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;
39
40 import javax.ws.rs.core.Response;
41
42 @Service("activitySpecs")
43 @Validated
44 public class ActivitySpecsImpl implements ActivitySpecs {
45
46
47     private final ActivitySpecManager activitySpecManager =
48             new ActivitySpecManagerImpl(ItemManagerFactory.getInstance().createInterface(),
49                                                VersioningManagerFactory.getInstance().createInterface(),
50                                                new ActivitySpecDaoZusammenImpl(ZusammenAdaptorFactory.getInstance()
51                                                                                                 .createInterface()),
52                                                UniqueValueDaoFactory.getInstance().createInterface());
53
54     @Override
55     public Response createActivitySpec(ActivitySpecRequestDto request) {
56         ActivitySpecEntity activitySpec =
57                 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
58
59         activitySpec = activitySpecManager.createActivitySpec(activitySpec);
60         ActivitySpecCreateResponse createActivitySpecResponse = new MapActivitySpecToActivitySpecCreateResponse()
61                                                                         .applyMapping(activitySpec,
62                                                                                 ActivitySpecCreateResponse.class);
63
64         return Response.ok(createActivitySpecResponse).build();
65     }
66
67     @Override
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();
76     }
77
78     @Override
79     public Response updateActivitySpec(ActivitySpecRequestDto request, String activitySpecId, String versionId) {
80         ActivitySpecEntity activitySpec =
81                 new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);
82
83         activitySpec.setId(activitySpecId);
84         activitySpec.setVersion(new Version(versionId));
85
86         activitySpecManager.update(activitySpec);
87
88         return Response.ok(new InternalEmptyObject()).build();
89     }
90
91     @Override
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")
95                                                                     .build());
96         }
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(mapper.applyMapping(activitySpecItem,
109                                       ActivitySpecListResponseDto.class)));
110
111         return Response.ok(results).build();
112     }
113 }