Commit 731dcbb9 authored by Matija Obreza's avatar Matija Obreza
Browse files

Use ClassPkService

parent a6108dec
...@@ -23,14 +23,12 @@ import java.util.List; ...@@ -23,14 +23,12 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import javax.persistence.PersistenceException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.model.ClassPK; import org.genesys.blocks.model.ClassPK;
import org.genesys.blocks.model.EntityId; import org.genesys.blocks.model.EntityId;
import org.genesys.blocks.persistence.ClassPKRepository;
import org.genesys2.server.model.impl.ActivityPost; import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.Article; import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Menu; import org.genesys2.server.model.impl.Menu;
...@@ -53,6 +51,7 @@ import org.springframework.data.domain.Pageable; ...@@ -53,6 +51,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@Service @Service
...@@ -67,7 +66,7 @@ public class ContentServiceImpl implements ContentService { ...@@ -67,7 +66,7 @@ public class ContentServiceImpl implements ContentService {
private ArticleRepository articleRepository; private ArticleRepository articleRepository;
@Autowired @Autowired
private ClassPKRepository classPkRepository; private ClassPKService classPkService;
@Autowired @Autowired
private HtmlSanitizer htmlSanitizer; private HtmlSanitizer htmlSanitizer;
...@@ -140,7 +139,7 @@ public class ContentServiceImpl implements ContentService { ...@@ -140,7 +139,7 @@ public class ContentServiceImpl implements ContentService {
@Override @Override
public Article getArticleBySlugLangTargetIdClassPk(String slug, String lang, Long targetId, String classPKShortName) { public Article getArticleBySlugLangTargetIdClassPk(String slug, String lang, Long targetId, String classPKShortName) {
ClassPK classPK = classPkRepository.findByShortName(classPKShortName); ClassPK classPK = classPkService.findByShortName(classPKShortName);
return articleRepository.findBySlugAndLangAndTargetIdAndClassPk(slug, lang, targetId, classPK); return articleRepository.findBySlugAndLangAndTargetIdAndClassPk(slug, lang, targetId, classPK);
} }
...@@ -241,60 +240,18 @@ public class ContentServiceImpl implements ContentService { ...@@ -241,60 +240,18 @@ public class ContentServiceImpl implements ContentService {
@Override @Override
public ClassPK getClassPk(Class<?> clazz) { public ClassPK getClassPk(Class<?> clazz) {
return classPkRepository.getByClassname(clazz.getName()); return classPkService.getClassPk(clazz);
} }
@Override @Override
public ClassPK getClassPk(String shortName) { public ClassPK getClassPk(String shortName) {
return classPkRepository.findByShortName(shortName); return classPkService.findByShortName(shortName);
} }
@Override @Override
@Transactional @Transactional(isolation = Isolation.READ_UNCOMMITTED)
public ClassPK ensureClassPK(Class<?> clazz) { public synchronized ClassPK ensureClassPK(Class<?> clazz) {
ClassPK classPk = classPkRepository.getByClassname(clazz.getName()); return classPkService.getClassPk(clazz);
if (classPk == null) {
classPk = ensureClassPKInternal(clazz);
}
if (classPk.getShortName() == null) {
classPk.setShortName(generateShortPkName(clazz.getSimpleName().toLowerCase()));
classPkRepository.save(classPk);
}
return classPk;
}
private synchronized ClassPK ensureClassPKInternal(Class<?> clazz) {
final String className = clazz.getName();
final String shortName = generateShortPkName(clazz.getSimpleName().toLowerCase());
ClassPK classPk = classPkRepository.getByClassname(className);
if (classPk == null) {
LOG.info("Registering new ClassPK for {}", className);
classPk = new ClassPK();
classPk.setClassname(className);
classPk.setShortName(shortName);
classPkRepository.save(classPk);
}
return classPk;
}
private String generateShortPkName(String shortName) {
ClassPK classPk = null;
String candidate = shortName;
int i = 0;
do {
classPk = classPkRepository.findByShortName(candidate);
if (classPk == null) {
// No other candidate, we're good!
return candidate;
}
i++;
// "shortName1", "shortName2"...
candidate = shortName + i;
} while (i < 10);
throw new PersistenceException("Can't generate a shortName for ClassPK for " + shortName);
} }
@Override @Override
......
...@@ -27,6 +27,8 @@ import com.hazelcast.monitor.LocalMapStats; ...@@ -27,6 +27,8 @@ import com.hazelcast.monitor.LocalMapStats;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException; import org.apache.velocity.exception.VelocityException;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.security.service.CustomAclService; import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.impl.CustomAclServiceImpl; import org.genesys.blocks.security.service.impl.CustomAclServiceImpl;
import org.genesys2.server.aspect.AsAdminAspect; import org.genesys2.server.aspect.AsAdminAspect;
...@@ -135,6 +137,11 @@ public class AccessionCacheTest { ...@@ -135,6 +137,11 @@ public class AccessionCacheTest {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public VelocityEngine velocityEngine() throws VelocityException, IOException { public VelocityEngine velocityEngine() throws VelocityException, IOException {
final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean(); final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean();
......
...@@ -29,6 +29,8 @@ import com.hazelcast.core.IQueue; ...@@ -29,6 +29,8 @@ import com.hazelcast.core.IQueue;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException; import org.apache.velocity.exception.VelocityException;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.security.service.CustomAclService; import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.impl.CustomAclServiceImpl; import org.genesys.blocks.security.service.impl.CustomAclServiceImpl;
import org.genesys2.server.aspect.AsAdminAspect; import org.genesys2.server.aspect.AsAdminAspect;
...@@ -147,6 +149,11 @@ public class ElasticsearchTest { ...@@ -147,6 +149,11 @@ public class ElasticsearchTest {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public VelocityEngine velocityEngine() throws VelocityException, IOException { public VelocityEngine velocityEngine() throws VelocityException, IOException {
final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean(); final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean();
......
...@@ -27,6 +27,8 @@ import com.hazelcast.core.IQueue; ...@@ -27,6 +27,8 @@ import com.hazelcast.core.IQueue;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.collect.Sets;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.security.persistence.AclClassPersistence; import org.genesys.blocks.security.persistence.AclClassPersistence;
import org.genesys.blocks.security.persistence.AclEntryPersistence; import org.genesys.blocks.security.persistence.AclEntryPersistence;
import org.genesys.blocks.security.persistence.AclObjectIdentityPersistence; import org.genesys.blocks.security.persistence.AclObjectIdentityPersistence;
...@@ -214,6 +216,11 @@ public abstract class GenesysServicesTest extends BaseSpringTest { ...@@ -214,6 +216,11 @@ public abstract class GenesysServicesTest extends BaseSpringTest {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public HtmlSanitizer htmlSanitizer() { public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer(); return new OWASPSanitizer();
......
...@@ -27,6 +27,8 @@ import java.util.List; ...@@ -27,6 +27,8 @@ import java.util.List;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException; import org.apache.velocity.exception.VelocityException;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.security.service.CustomAclService; import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.impl.CustomAclServiceImpl; import org.genesys.blocks.security.service.impl.CustomAclServiceImpl;
import org.genesys2.server.aspect.AsAdminAspect; import org.genesys2.server.aspect.AsAdminAspect;
...@@ -137,6 +139,11 @@ public class SGSVUpdateTest { ...@@ -137,6 +139,11 @@ public class SGSVUpdateTest {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public VelocityEngine velocityEngine() throws VelocityException, IOException { public VelocityEngine velocityEngine() throws VelocityException, IOException {
final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean(); final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean();
......
...@@ -30,6 +30,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -30,6 +30,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException; import org.apache.velocity.exception.VelocityException;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.security.service.CustomAclService; import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.impl.CustomAclServiceImpl; import org.genesys.blocks.security.service.impl.CustomAclServiceImpl;
import org.genesys2.server.aspect.AsAdminAspect; import org.genesys2.server.aspect.AsAdminAspect;
...@@ -142,6 +144,11 @@ public class BatchRESTServiceTest { ...@@ -142,6 +144,11 @@ public class BatchRESTServiceTest {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public VelocityEngine velocityEngine() throws VelocityException, IOException { public VelocityEngine velocityEngine() throws VelocityException, IOException {
final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean(); final VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean();
......
...@@ -21,6 +21,8 @@ import java.util.Properties; ...@@ -21,6 +21,8 @@ import java.util.Properties;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException; import org.apache.velocity.exception.VelocityException;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.security.service.CustomAclService; import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.impl.CustomAclServiceImpl; import org.genesys.blocks.security.service.impl.CustomAclServiceImpl;
import org.genesys2.server.aspect.AsAdminAspect; import org.genesys2.server.aspect.AsAdminAspect;
...@@ -138,6 +140,11 @@ public class GenesysBeansConfig { ...@@ -138,6 +140,11 @@ public class GenesysBeansConfig {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public VelocityEngine velocityEngine() throws VelocityException, IOException { public VelocityEngine velocityEngine() throws VelocityException, IOException {
final VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean(); final VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
......
...@@ -28,6 +28,8 @@ import javax.mail.internet.MimeMessage; ...@@ -28,6 +28,8 @@ import javax.mail.internet.MimeMessage;
import org.apache.commons.lang.math.RandomUtils; import org.apache.commons.lang.math.RandomUtils;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException; import org.apache.velocity.exception.VelocityException;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys2.server.listener.sample.CreateContentListener; import org.genesys2.server.listener.sample.CreateContentListener;
import org.genesys2.server.model.genesys.MaterialRequest; import org.genesys2.server.model.genesys.MaterialRequest;
import org.genesys2.server.persistence.domain.MaterialRequestRepository; import org.genesys2.server.persistence.domain.MaterialRequestRepository;
...@@ -100,6 +102,11 @@ public class MaterialRequestTest { ...@@ -100,6 +102,11 @@ public class MaterialRequestTest {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public HtmlSanitizer htmlSanitizer() { public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer(); return new OWASPSanitizer();
......
...@@ -26,6 +26,8 @@ import com.hazelcast.core.IQueue; ...@@ -26,6 +26,8 @@ import com.hazelcast.core.IQueue;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.collect.Sets;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.oauth.persistence.AccessTokenRepository; import org.genesys.blocks.oauth.persistence.AccessTokenRepository;
import org.genesys.blocks.oauth.persistence.OAuthClientRepository; import org.genesys.blocks.oauth.persistence.OAuthClientRepository;
import org.genesys.blocks.oauth.persistence.RefreshTokenRepository; import org.genesys.blocks.oauth.persistence.RefreshTokenRepository;
...@@ -273,6 +275,11 @@ public abstract class AbstractRestTest extends BaseSpringTest { ...@@ -273,6 +275,11 @@ public abstract class AbstractRestTest extends BaseSpringTest {
public ContentService contentService() { public ContentService contentService() {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public HtmlSanitizer htmlSanitizer() { public HtmlSanitizer htmlSanitizer() {
......
...@@ -21,6 +21,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -21,6 +21,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.elasticsearch.common.collect.Sets; import org.elasticsearch.common.collect.Sets;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.auditlog.service.impl.ClassPKServiceImpl;
import org.genesys.blocks.persistence.ClassPKRepository; import org.genesys.blocks.persistence.ClassPKRepository;
import org.genesys.blocks.security.service.CustomAclService; import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.PasswordPolicy; import org.genesys.blocks.security.service.PasswordPolicy;
...@@ -131,6 +133,11 @@ public abstract class AbstractServicesTest extends BaseSpringTest { ...@@ -131,6 +133,11 @@ public abstract class AbstractServicesTest extends BaseSpringTest {
return new ContentServiceImpl(); return new ContentServiceImpl();
} }
@Bean
public ClassPKService classPkService() {
return new ClassPKServiceImpl();
}
@Bean @Bean
public HtmlSanitizer htmlSanitizer() { public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer(); return new OWASPSanitizer();
......
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