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 {
* @param className
* @return
*/
Long getClassPkId(String className);
Long getClassPkId(String className) throws ClassNotFoundException;
}
......@@ -53,15 +53,7 @@ public class ClassPKServiceImpl implements ClassPKService {
if (classPk != null) {
return classPk;
}
classPk = new ClassPK();
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);
return createNewClassPkForClass(class1);
}
private String generateShortPkName(String shortName) {
......@@ -84,7 +76,6 @@ public class ClassPKServiceImpl implements ClassPKService {
throw new PersistenceException("Can't generate a shortName for ClassPK for " + shortName);
}
@Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public String getClassName(final Long id) {
......@@ -98,7 +89,33 @@ public class ClassPKServiceImpl implements ClassPKService {
}
@Override
public Long getClassPkId(String classname) {
return classPkRepository.getClassPkId(classname);
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
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