-
Notifications
You must be signed in to change notification settings - Fork 45
/
RepresentationApplicationService.java
77 lines (68 loc) · 3.55 KB
/
RepresentationApplicationService.java
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
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.representation.services;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import org.eclipse.sirius.components.core.RepresentationMetadata;
import org.eclipse.sirius.web.application.UUIDParser;
import org.eclipse.sirius.web.application.representation.services.api.IRepresentationApplicationService;
import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Used to interact with representations.
*
* @author sbegaudeau
*/
@Service
public class RepresentationApplicationService implements IRepresentationApplicationService {
private final IRepresentationDataSearchService representationDataSearchService;
public RepresentationApplicationService(IRepresentationDataSearchService representationDataSearchService) {
this.representationDataSearchService = Objects.requireNonNull(representationDataSearchService);
}
@Override
@Transactional(readOnly = true)
public Optional<RepresentationMetadata> findRepresentationMetadataById(String representationId) {
return new UUIDParser().parse(representationId)
.flatMap(this.representationDataSearchService::findById)
.map(this::toRepresentationMetadata);
}
@Override
public Page<RepresentationMetadata> findAllByEditingContextId(String editingContextId, Pageable pageable) {
var representationData = new UUIDParser().parse(editingContextId)
.map(AggregateReference::<Project, UUID>to)
.map(this.representationDataSearchService::findAllByProject)
.orElse(List.of())
.stream()
.sorted(Comparator.comparing(RepresentationData::getLabel))
.toList();
int startIndex = (int) pageable.getOffset() * pageable.getPageSize();
int endIndex = Math.min(((int) pageable.getOffset() + 1) * pageable.getPageSize(), representationData.size());
var representationMetadata = representationData.subList(startIndex, endIndex).stream()
.map(this::toRepresentationMetadata)
.toList();
return new PageImpl<>(representationMetadata, pageable, representationData.size());
}
private RepresentationMetadata toRepresentationMetadata(RepresentationData representationData) {
return new RepresentationMetadata(representationData.getId().toString(), representationData.getKind(), representationData.getLabel(), representationData.getDescriptionId());
}
}