Skip to content

Commit 639af46

Browse files
Merge pull request #7040 from davidwatkins73/waltz-7039-licence-search
Licences - search for licences from navbar
2 parents 1c4781b + d831a25 commit 639af46

7 files changed

Lines changed: 137 additions & 5 deletions

File tree

waltz-common/src/main/java/org/finos/waltz/common/ListUtilities.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,23 @@ public static <T> Optional<T> maybeGet(List<T> xs, int idx) {
195195
}
196196
}
197197

198+
198199
/**
199200
* Given a list, index and default value returns the element at that index or the default value if the index is out of bounds.
200201
*/
201202
public static <T> T getOrDefault(List<T> xs, int idx, T defaultValue) {
202203
return maybeGet(xs, idx)
203204
.orElse(defaultValue);
204205
}
206+
207+
208+
/**
209+
* Returns a list of distinct values from a given list
210+
* @param ts a list of T's with possible duplicates
211+
* @return distinct values in <code>ts</code>
212+
* @param <T>
213+
*/
214+
public static <T> List<T> distinct(List<T> ts) {
215+
return ts.stream().distinct().collect(toList());
216+
}
205217
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.finos.waltz.common;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
7+
import static org.finos.waltz.common.ListUtilities.asList;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class ListUtilities_distinct {
11+
12+
@Test
13+
public void distinctDedupesListsButPreservesInitialOrder() {
14+
List<String> xs = asList("a", "b", "c", "c", "b", "c", "d");
15+
List<String> uniq = ListUtilities.distinct(xs);
16+
assertEquals(asList("a", "b", "c", "d"), uniq);
17+
}
18+
19+
20+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.finos.waltz.data.licence.search;
2+
3+
import org.finos.waltz.common.ListUtilities;
4+
import org.finos.waltz.data.SearchDao;
5+
import org.finos.waltz.data.legal_entity.LegalEntityDao;
6+
import org.finos.waltz.data.licence.LicenceDao;
7+
import org.finos.waltz.model.entity_search.EntitySearchOptions;
8+
import org.finos.waltz.model.legal_entity.LegalEntity;
9+
import org.finos.waltz.model.licence.Licence;
10+
import org.jooq.Condition;
11+
import org.jooq.DSLContext;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.stereotype.Service;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.TreeSet;
18+
19+
import static java.util.Collections.emptyList;
20+
import static org.finos.waltz.common.ListUtilities.concat;
21+
import static org.finos.waltz.data.JooqUtilities.mkBasicTermSearch;
22+
import static org.finos.waltz.data.JooqUtilities.mkStartsWithTermSearch;
23+
import static org.finos.waltz.data.SearchUtilities.mkTerms;
24+
import static org.finos.waltz.schema.Tables.LICENCE;
25+
26+
@Service
27+
public class LicenceSearchDao implements SearchDao<Licence> {
28+
29+
private final DSLContext dsl;
30+
31+
32+
@Autowired
33+
public LicenceSearchDao(DSLContext dsl) {
34+
this.dsl = dsl;
35+
}
36+
37+
38+
/**
39+
* Searches by <code>name</code> and <code>external_id</code>
40+
* @param options
41+
* @return List of matching legal entities,
42+
* matches on name are given precedence over external_id matches
43+
*/
44+
@Override
45+
public List<Licence> search(EntitySearchOptions options) {
46+
List<String> terms = mkTerms(options.searchQuery());
47+
if (terms.isEmpty()) {
48+
return emptyList();
49+
}
50+
51+
Condition nameCondition = mkBasicTermSearch(LICENCE.NAME, terms);
52+
Condition externalIdCondition = mkStartsWithTermSearch(LICENCE.EXTERNAL_ID, terms);
53+
54+
return ListUtilities.distinct(concat(
55+
mkQuery(nameCondition, options),
56+
mkQuery(externalIdCondition, options)));
57+
}
58+
59+
60+
private List<Licence> mkQuery(Condition nameCondition, EntitySearchOptions options) {
61+
return dsl
62+
.select(LICENCE.fields())
63+
.from(LICENCE)
64+
.where(nameCondition)
65+
.orderBy(LICENCE.NAME)
66+
.limit(options.limit())
67+
.fetch(LicenceDao.TO_DOMAIN_MAPPER);
68+
}
69+
70+
}

waltz-model/src/main/java/org/finos/waltz/model/licence/Licence.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,16 @@ public abstract class Licence implements
3434
ExternalIdProvider,
3535
CreatedUserTimestampProvider,
3636
LastUpdatedUserTimestampProvider,
37-
ProvenanceProvider {
37+
ProvenanceProvider,
38+
WaltzEntity {
39+
40+
@Override
41+
public EntityReference entityReference() {
42+
return EntityReference.mkRef(
43+
EntityKind.LICENCE,
44+
id().get(),
45+
name(),
46+
description(),
47+
externalId().orElse(""));
48+
}
3849
}

waltz-ng/client/navbar/components/nav-search-overlay/nav-search-overlay.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ const initialState = {
5252
entity.DATABASE.key,
5353
entity.SOFTWARE.key,
5454
entity.ROADMAP.key,
55-
entity.LOGICAL_DATA_ELEMENT.key
55+
entity.LOGICAL_DATA_ELEMENT.key,
56+
entity.LICENCE.key
5657
],
5758
selectedCategory: null,
5859
showActiveOnly: true,
@@ -149,7 +150,7 @@ function controller($element,
149150
.then(() => handleSearch(query, [entity.APP_GROUP.key, entity.CHANGE_INITIATIVE.key, entity.ORG_UNIT.key]))
150151
.then(() => handleSearch(query, [entity.ACTOR.key, entity.MEASURABLE.key, entity.LEGAL_ENTITY.key]))
151152
.then(() => handleSearch(query, [entity.PHYSICAL_SPECIFICATION.key, entity.DATA_TYPE.key, entity.SERVER.key, entity.DATABASE.key]))
152-
.then(() => handleSearch(query, [entity.SOFTWARE.key, entity.ROADMAP.key, entity.LOGICAL_DATA_ELEMENT.key]))
153+
.then(() => handleSearch(query, [entity.SOFTWARE.key, entity.ROADMAP.key, entity.LOGICAL_DATA_ELEMENT.key, entity.LICENCE.key]))
153154
.catch(e => displayError("Failed to search"))
154155
.finally(() => vm.searching = false);
155156
};

waltz-service/src/main/java/org/finos/waltz/service/entity_search/EntitySearchService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.finos.waltz.service.database_information.DatabaseInformationService;
2727
import org.finos.waltz.service.flow_diagram.FlowDiagramService;
2828
import org.finos.waltz.service.legal_entity.LegalEntityService;
29+
import org.finos.waltz.service.licence.LicenceService;
2930
import org.finos.waltz.service.logical_data_element.LogicalDataElementService;
3031
import org.finos.waltz.service.measurable.MeasurableService;
3132
import org.finos.waltz.service.orgunit.OrganisationalUnitService;
@@ -74,6 +75,7 @@ public class EntitySearchService {
7475
private final FlowDiagramService flowDiagramService;
7576
private final LegalEntityService legalEntityService;
7677
private final DatabaseInformationService databaseInformationService;
78+
private final LicenceService licenceService;
7779

7880

7981
@Autowired
@@ -93,7 +95,8 @@ public EntitySearchService(DBExecutorPoolInterface dbExecutorPool,
9395
SoftwareCatalogService softwareCatalogService,
9496
FlowDiagramService flowDiagramService,
9597
LegalEntityService legalEntityService,
96-
DatabaseInformationService databaseInformationService) {
98+
DatabaseInformationService databaseInformationService,
99+
LicenceService licenceService) {
97100

98101
checkNotNull(dbExecutorPool, "dbExecutorPool cannot be null");
99102
checkNotNull(actorService, "actorService cannot be null");
@@ -112,6 +115,7 @@ public EntitySearchService(DBExecutorPoolInterface dbExecutorPool,
112115
checkNotNull(softwareCatalogService, "softwareCatalogService cannot be null");
113116
checkNotNull(legalEntityService, "legalEntityService cannot be null");
114117
checkNotNull(databaseInformationService, "databaseInformationService cannot be null");
118+
checkNotNull(licenceService, "licenceService cannot be null");
115119

116120
this.actorService = actorService;
117121
this.dbExecutorPool = dbExecutorPool;
@@ -130,6 +134,7 @@ public EntitySearchService(DBExecutorPoolInterface dbExecutorPool,
130134
this.softwareCatalogService = softwareCatalogService;
131135
this.legalEntityService = legalEntityService;
132136
this.databaseInformationService = databaseInformationService;
137+
this.licenceService = licenceService;
133138
}
134139

135140

@@ -174,6 +179,8 @@ private Callable<Collection<? extends WaltzEntity>> mkCallable(EntityKind entity
174179
return () -> flowDiagramService.search(options);
175180
case LEGAL_ENTITY:
176181
return () -> legalEntityService.search(options);
182+
case LICENCE:
183+
return () -> licenceService.search(options);
177184
case LOGICAL_DATA_ELEMENT:
178185
return () -> logicalDataElementService.search(options);
179186
case MEASURABLE:

waltz-service/src/main/java/org/finos/waltz/service/licence/LicenceService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
import org.finos.waltz.data.licence.LicenceDao;
2323
import org.finos.waltz.data.licence.LicenceIdSelectorFactory;
24+
import org.finos.waltz.data.licence.search.LicenceSearchDao;
2425
import org.finos.waltz.model.IdSelectionOptions;
26+
import org.finos.waltz.model.entity_search.EntitySearchOptions;
2527
import org.finos.waltz.model.licence.Licence;
2628
import org.finos.waltz.model.licence.SaveLicenceCommand;
2729
import org.finos.waltz.model.tally.Tally;
@@ -30,6 +32,7 @@
3032
import org.springframework.beans.factory.annotation.Autowired;
3133
import org.springframework.stereotype.Service;
3234

35+
import java.util.Collection;
3336
import java.util.List;
3437

3538
import static org.finos.waltz.common.Checks.checkNotNull;
@@ -38,13 +41,17 @@
3841
public class LicenceService {
3942

4043
private final LicenceDao licenceDao;
44+
private final LicenceSearchDao licenceSearchDao;
4145
private final LicenceIdSelectorFactory licenceIdSelectorFactory = new LicenceIdSelectorFactory();
4246

4347

4448
@Autowired
45-
public LicenceService(LicenceDao licenceDao) {
49+
public LicenceService(LicenceDao licenceDao,
50+
LicenceSearchDao licenceSearchDao) {
4651
checkNotNull(licenceDao, "licenceDao cannot be null");
52+
checkNotNull(licenceSearchDao, "licenceSearchDao cannot be null");
4753
this.licenceDao = licenceDao;
54+
this.licenceSearchDao = licenceSearchDao;
4855
}
4956

5057

@@ -81,4 +88,8 @@ public boolean save(SaveLicenceCommand cmd, String username) {
8188
public boolean remove(long licenceId, String username) {
8289
return licenceDao.remove(licenceId);
8390
}
91+
92+
public Collection<Licence> search(EntitySearchOptions options) {
93+
return licenceSearchDao.search(options);
94+
}
8495
}

0 commit comments

Comments
 (0)