summaryrefslogtreecommitdiffstats
path: root/services/activity-spec/activity-spec-web/activity-spec-service/src/main/java/org/onap/sdc/activityspec/api/rest/services/ActivitySpecsImpl.java
blob: e0eec80fcb01e252fa65190a5851ed74ef70b527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.sdc.activityspec.api.rest.services;

import org.onap.sdc.activityspec.api.rest.ActivitySpecs;
import org.onap.sdc.activityspec.api.rest.mapping.MapActivitySpecRequestDtoToActivitySpecEntity;
import org.onap.sdc.activityspec.api.rest.mapping.MapActivitySpecToActivitySpecCreateResponse;
import org.onap.sdc.activityspec.api.rest.mapping.MapActivitySpecToActivitySpecGetResponse;
import org.onap.sdc.activityspec.api.rest.mapping.MapItemToListResponseDto;
import org.onap.sdc.activityspec.api.rest.types.ActivitySpecActionRequestDto;
import org.onap.sdc.activityspec.api.rest.types.ActivitySpecGetResponse;
import org.onap.sdc.activityspec.api.rest.types.ActivitySpecListResponseDto;
import org.onap.sdc.activityspec.api.rest.types.ActivitySpecRequestDto;
import org.onap.sdc.activityspec.api.rest.types.InternalEmptyObject;
import org.onap.sdc.activityspec.be.ActivitySpecManager;
import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity;
import org.onap.sdc.activityspec.be.impl.ActivitySpecManagerImpl;
import org.onap.sdc.activityspec.api.rest.types.ActivitySpecCreateResponse;
import org.onap.sdc.activityspec.be.dao.impl.ActivitySpecDaoZusammenImpl;
import org.openecomp.core.dao.UniqueValueDaoFactory;
import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory;
import org.openecomp.sdc.versioning.ItemManagerFactory;
import org.openecomp.sdc.versioning.VersioningManagerFactory;
import org.openecomp.sdc.versioning.dao.types.Version;

import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.ws.rs.core.Response;

@Service("activitySpecs")
@Validated
public class ActivitySpecsImpl implements ActivitySpecs {


    private final ActivitySpecManager activitySpecManager =
            new ActivitySpecManagerImpl(ItemManagerFactory.getInstance().createInterface(),
                                               VersioningManagerFactory.getInstance().createInterface(),
                                               new ActivitySpecDaoZusammenImpl(ZusammenAdaptorFactory.getInstance()
                                                                                                .createInterface()),
                                               UniqueValueDaoFactory.getInstance().createInterface());

    @Override
    public Response createActivitySpec(ActivitySpecRequestDto request) {
        ActivitySpecEntity activitySpec =
                new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);

        activitySpec = activitySpecManager.createActivitySpec(activitySpec);
        ActivitySpecCreateResponse createActivitySpecResponse = new MapActivitySpecToActivitySpecCreateResponse()
                                                                        .applyMapping(activitySpec,
                                                                                ActivitySpecCreateResponse.class);

        return Response.ok(createActivitySpecResponse).build();
    }

    @Override
    public Response getActivitySpec(String activitySpecId, String versionId) {
        ActivitySpecEntity activitySpec = new ActivitySpecEntity();
        activitySpec.setId(activitySpecId);
        activitySpec.setVersion(new Version(versionId));
        final ActivitySpecEntity retrieved = activitySpecManager.get(activitySpec);
        ActivitySpecGetResponse getResponse =
                new MapActivitySpecToActivitySpecGetResponse().applyMapping(retrieved, ActivitySpecGetResponse.class);
        return Response.ok(getResponse).build();
    }

    @Override
    public Response updateActivitySpec(ActivitySpecRequestDto request, String activitySpecId, String versionId) {
        ActivitySpecEntity activitySpec =
                new MapActivitySpecRequestDtoToActivitySpecEntity().applyMapping(request, ActivitySpecEntity.class);

        activitySpec.setId(activitySpecId);
        activitySpec.setVersion(new Version(versionId));

        activitySpecManager.update(activitySpec);

        return Response.ok(new InternalEmptyObject()).build();
    }

    @Override
    public Response actOnActivitySpec(ActivitySpecActionRequestDto request, String activitySpecId, String versionId) {
        activitySpecManager.actOnAction(activitySpecId, versionId, request.getAction());
        return Response.ok(new InternalEmptyObject()).build();
    }

    @Override
    public Response list(String versionStatus) {

        GenericCollectionWrapper<ActivitySpecListResponseDto> results = new GenericCollectionWrapper<>();
        MapItemToListResponseDto mapper = new MapItemToListResponseDto();
        activitySpecManager.list(versionStatus).stream()
                           .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())).forEach(
                activitySpecItem -> results.add(
                        mapper.applyMapping(activitySpecItem, ActivitySpecListResponseDto.class)));

        return Response.ok(results).build();
    }

}