Commit 88449b5c authored by Andrey Lugovskiy's avatar Andrey Lugovskiy Committed by Matija Obreza
Browse files

Create new classPk if it doesn't exist

parent 9a861ab5
...@@ -48,6 +48,6 @@ public interface ClassPKService { ...@@ -48,6 +48,6 @@ public interface ClassPKService {
* @param className * @param className
* @return * @return
*/ */
Long getClassPkId(String className); Long getClassPkId(String className) throws ClassNotFoundException;
} }
...@@ -53,15 +53,7 @@ public class ClassPKServiceImpl implements ClassPKService { ...@@ -53,15 +53,7 @@ public class ClassPKServiceImpl implements ClassPKService {
if (classPk != null) { if (classPk != null) {
return classPk; return classPk;
} }
classPk = new ClassPK(); return createNewClassPkForClass(class1);
classPk.setClassname(class1.getName());
classPk.setShortName(generateShortPkName(class1.getSimpleName().toLowerCase()));
if (LOG.isTraceEnabled()) {
LOG.trace("Persisting new ClassPK for classname={}", classPk.getClassname());
}
return classPkRepository.save(classPk);
} }
private String generateShortPkName(String shortName) { private String generateShortPkName(String shortName) {
...@@ -84,7 +76,6 @@ public class ClassPKServiceImpl implements ClassPKService { ...@@ -84,7 +76,6 @@ public class ClassPKServiceImpl implements ClassPKService {
throw new PersistenceException("Can't generate a shortName for ClassPK for " + shortName); throw new PersistenceException("Can't generate a shortName for ClassPK for " + shortName);
} }
@Override @Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW) @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public String getClassName(final Long id) { public String getClassName(final Long id) {
...@@ -98,7 +89,33 @@ public class ClassPKServiceImpl implements ClassPKService { ...@@ -98,7 +89,33 @@ public class ClassPKServiceImpl implements ClassPKService {
} }
@Override @Override
public Long getClassPkId(String classname) { @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
return classPkRepository.getClassPkId(classname); public Long getClassPkId(String classname) throws ClassNotFoundException {
Long id = null;
try {
id = classPkRepository.getClassPkId(classname);
if (id == null) {
throw new Exception();
}
} catch (Exception e) {
Class aClass = Class.forName(classname);
ClassPK classPK = createNewClassPkForClass(aClass);
id = classPK.getId();
}
return id;
}
private ClassPK createNewClassPkForClass(Class<?> aClass) {
ClassPK classPk = new ClassPK();
classPk.setClassname(aClass.getName());
classPk.setShortName(generateShortPkName(aClass.getSimpleName().toLowerCase()));
if (LOG.isTraceEnabled()) {
LOG.trace("Persisting new ClassPK for classname={}", classPk.getClassname());
}
return classPkRepository.save(classPk);
} }
} }
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