Search in sources :

Example 1 with UserUsageConditions

use of org.haiku.haikudepotserver.dataobjects.UserUsageConditions in project haikudepotserver by haiku.

the class UserApiImpl method getUserUsageConditions.

@Override
public GetUserUsageConditionsResult getUserUsageConditions(GetUserUsageConditionsRequest request) {
    Preconditions.checkNotNull(request);
    final ObjectContext context = serverRuntime.newContext();
    UserUsageConditions userUsageConditions = StringUtils.isNotBlank(request.code) ? UserUsageConditions.getByCode(context, request.code) : UserUsageConditions.getLatest(context);
    GetUserUsageConditionsResult result = new GetUserUsageConditionsResult();
    result.code = userUsageConditions.getCode();
    result.minimumAge = userUsageConditions.getMinimumAge();
    return result;
}
Also used : UserUsageConditions(org.haiku.haikudepotserver.dataobjects.UserUsageConditions) ObjectContext(org.apache.cayenne.ObjectContext)

Example 2 with UserUsageConditions

use of org.haiku.haikudepotserver.dataobjects.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)

Example 3 with UserUsageConditions

use of org.haiku.haikudepotserver.dataobjects.UserUsageConditions in project haikudepotserver by haiku.

the class UserUsageConditionsInitializer method initUserUsageConditions.

private void initUserUsageConditions(ObjectContext context, Pair<Resource, Resource> resources, int ordering) {
    Resource metaDataResource = resources.getLeft();
    Resource markdownResource = resources.getRight();
    if (null == metaDataResource || !metaDataResource.exists()) {
        throw new IllegalStateException("unable to find the meta data resource");
    }
    if (null == markdownResource || !markdownResource.exists()) {
        throw new IllegalStateException("unable to find the markdown resource");
    }
    try (InputStream metaDataInputStream = metaDataResource.getInputStream();
        InputStream markdownInputStream = markdownResource.getInputStream()) {
        UserUsageConditionsMetaData metaData = objectMapper.readValue(metaDataInputStream, UserUsageConditionsMetaData.class);
        String markdownString = StreamUtils.copyToString(markdownInputStream, Charsets.UTF_8);
        UserUsageConditions userUsageConditions = context.newObject(UserUsageConditions.class);
        userUsageConditions.setCode(Files.getNameWithoutExtension(metaDataResource.getFilename()));
        userUsageConditions.setCopyMarkdown(markdownString);
        userUsageConditions.setMinimumAge(metaData.getMinimumAge());
        userUsageConditions.setOrdering(ordering);
        LOGGER.info("will create user usage conditions [{}]", userUsageConditions.getCode());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions(org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions) UserUsageConditions(org.haiku.haikudepotserver.dataobjects.UserUsageConditions) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 4 with UserUsageConditions

use of org.haiku.haikudepotserver.dataobjects.UserUsageConditions in project haikudepotserver by haiku.

the class UserController method handleGetUserUsageConditionsHtml.

@RequestMapping(value = "/" + SEGMENT_USER + "/usageconditions/{" + KEY_CODE + "}/document.html", method = RequestMethod.GET)
public void handleGetUserUsageConditionsHtml(HttpServletResponse response, @PathVariable(value = KEY_CODE) String code) throws IOException {
    Optional<UserUsageConditions> markdownOptional = tryGetUserUsageConditions(code);
    if (!markdownOptional.isPresent()) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return;
    }
    Document markdownDocument = markdownParser.parse(markdownOptional.get().getCopyMarkdown());
    byte[] payload = htmlRenderer.render(markdownDocument).getBytes(Charsets.UTF_8);
    response.setContentType(com.google.common.net.MediaType.HTML_UTF_8.toString());
    response.setContentLength(payload.length);
    try (OutputStream outputStream = response.getOutputStream()) {
        outputStream.write(payload);
    }
}
Also used : UserUsageConditions(org.haiku.haikudepotserver.dataobjects.UserUsageConditions) OutputStream(java.io.OutputStream) Document(com.vladsch.flexmark.util.ast.Document) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with UserUsageConditions

use of org.haiku.haikudepotserver.dataobjects.UserUsageConditions in project haikudepotserver by haiku.

the class UserController method handleGetUserUsageConditionsMarkdown.

@RequestMapping(value = "/" + SEGMENT_USER + "/usageconditions/{" + KEY_CODE + "}/document.md", method = RequestMethod.GET)
public void handleGetUserUsageConditionsMarkdown(HttpServletResponse response, @PathVariable(value = KEY_CODE) String code) throws IOException {
    Optional<UserUsageConditions> markdownOptional = tryGetUserUsageConditions(code);
    if (!markdownOptional.isPresent()) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return;
    }
    byte[] payload = markdownOptional.get().getCopyMarkdown().getBytes(Charsets.UTF_8);
    response.setContentType(MediaType.MEDIATYPE_MARKDOWN);
    response.setContentLength(payload.length);
    try (OutputStream outputStream = response.getOutputStream()) {
        outputStream.write(payload);
    }
}
Also used : UserUsageConditions(org.haiku.haikudepotserver.dataobjects.UserUsageConditions) OutputStream(java.io.OutputStream) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserUsageConditions (org.haiku.haikudepotserver.dataobjects.UserUsageConditions)6 ObjectContext (org.apache.cayenne.ObjectContext)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions (org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions)2 Resource (org.springframework.core.io.Resource)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 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 Document (com.vladsch.flexmark.util.ast.Document)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Set (java.util.Set)1