Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Genesys PGR
Genesys Backend
Commits
36af99f1
Commit
36af99f1
authored
Oct 16, 2013
by
Matija Obreza
Browse files
CreateContentListener checks for resources in default-contents package
and creates missing global articles
parent
854af0c1
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/genesys2/server/listener/sample/CreateContentListener.java
0 → 100644
View file @
36af99f1
/**
* 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
());
}
}
}
}
}
src/main/java/org/genesys2/server/listener/sample/default-content/account-created
0 → 100644
View file @
36af99f1
{
"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
src/main/java/org/genesys2/server/service/impl/ContentServiceImpl.java
View file @
36af99f1
...
...
@@ -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"
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment