Commit 36af99f1 authored by Matija Obreza's avatar Matija Obreza
Browse files

CreateContentListener checks for resources in default-contents package

and creates missing global articles
parent 854af0c1
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.listener.sample;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map.Entry;
import org.apache.commons.io.IOUtils;
import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Component
public class CreateContentListener extends RunAsAdminListener {
@Autowired
private ContentService contentService;
@Override
public void init() throws Exception {
_logger.info("Checking if default content exists");
ClassLoader classLoader = CreateContentListener.class.getClassLoader();
PathMatchingResourcePatternResolver rpr = new PathMatchingResourcePatternResolver(classLoader);
String resourcePath = CreateContentListener.class.getPackage().getName().replace('.', '/').concat("/default-content/*");
Resource[] rs = rpr.getResources(resourcePath);
for (Resource r : rs) {
_logger.info(r.getFilename());
String slug = r.getFilename();
ObjectMapper mapper = new ObjectMapper();
InputStream stream = r.getInputStream();
JsonNode json = mapper.readTree(stream);
IOUtils.closeQuietly(stream);
Iterator<Entry<String, JsonNode>> it = json.fields();
while (it.hasNext()) {
Entry<String, JsonNode> entry = it.next();
Locale locale = new Locale(entry.getKey());
// Load from default locale if exists
Article article = contentService.getGlobalArticle(slug, locale, false);
// If nothing is found, parse the resource and create content
if (article == null) {
contentService.createGlobalArticle(slug, locale, entry.getValue().get("title").asText(), entry.getValue().get("body").asText());
_logger.info("Created article for slug: " + slug + " lang=" + locale.getLanguage());
}
}
}
}
}
{
"en": {
"title": "Your user account is created",
"body": "The user account is now registered. You may use it to log in."
}, "sl": {
"title": "Registracija je uspela",
"body": "Vaše uporabniško ime je registrirano. Sedaj se lahko prijavite."
}
}
\ No newline at end of file
......@@ -125,7 +125,7 @@ public class ContentServiceImpl implements ContentService {
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Article createGlobalArticle(String slug, Locale locale, String title, String body) {
Article article = getGlobalArticle(slug, locale);
Article article = getGlobalArticle(slug, locale, false);
if (article != null) {
throw new RuntimeException("Article exists");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment