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;
}
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());
}
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);
}
}
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);
}
}
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);
}
}
Aggregations