Search in sources :

Example 1 with org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions

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());
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) MutableInt(org.apache.commons.lang3.mutable.MutableInt) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) HaikuDepot(org.haiku.haikudepotserver.dataobjects.HaikuDepot) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Pair(org.apache.commons.lang3.tuple.Pair) Files(com.google.common.io.Files) org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions(org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions) Resource(org.springframework.core.io.Resource) StreamUtils(org.springframework.util.StreamUtils) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) UserUsageConditions(org.haiku.haikudepotserver.dataobjects.UserUsageConditions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) InputStream(java.io.InputStream) org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions(org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions) UserUsageConditions(org.haiku.haikudepotserver.dataobjects.UserUsageConditions) org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions(org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions) MutableInt(org.apache.commons.lang3.mutable.MutableInt) UncheckedIOException(java.io.UncheckedIOException) ObjectContext(org.apache.cayenne.ObjectContext) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Aggregations

JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Charsets (com.google.common.base.Charsets)1 Preconditions (com.google.common.base.Preconditions)1 Files (com.google.common.io.Files)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)1 StringUtils (org.apache.commons.lang3.StringUtils)1 MutableInt (org.apache.commons.lang3.mutable.MutableInt)1 Pair (org.apache.commons.lang3.tuple.Pair)1 HaikuDepot (org.haiku.haikudepotserver.dataobjects.HaikuDepot)1