use of org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions in project haikudepotserver by haiku.
the class UserUsageConditionsInitializer method initUserUsageConditions.
/**
* <p>User usage conditions need to be loaded into the database. These are
* present as file-resources in the deployment. Here those files are loaded
* up and if they are not present in the database then the entries are
* populated.</p>
*/
private void initUserUsageConditions() {
ObjectContext context = serverRuntime.newContext();
List<UserUsageConditions> existingUserUsageConditions = UserUsageConditions.getAll(context);
Set<String> existingUserUsageConditionCodes = existingUserUsageConditions.stream().map(_UserUsageConditions::getCode).collect(Collectors.toSet());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
int initialOrdering = existingUserUsageConditions.stream().mapToInt(_UserUsageConditions::getOrdering).max().orElse(100);
MutableInt mutableOrdering = new MutableInt(initialOrdering);
try {
Arrays.stream(resolver.getResources(RESOURCE_PREFIX + "*.json")).filter(r -> StringUtils.isNotEmpty(r.getFilename())).filter(r -> !existingUserUsageConditionCodes.contains(Files.getNameWithoutExtension(r.getFilename()))).sorted(Comparator.comparing(Resource::getFilename)).map(r -> Pair.of(r, resolver.getResource(RESOURCE_PREFIX + Files.getNameWithoutExtension(r.getFilename()) + ".md"))).forEach(p -> initUserUsageConditions(context, p, mutableOrdering.incrementAndGet()));
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
if (mutableOrdering.getValue() > initialOrdering) {
LOGGER.info("did create {} user usage conditions", mutableOrdering.getValue() - initialOrdering);
context.commitChanges();
}
serverRuntime.getDataDomain().getQueryCache().removeGroup(HaikuDepot.CacheGroup.USER_USAGE_CONDITIONS.name());
}
Aggregations