Commit 04910c55 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '11-fix-getclasspkid' into 'master'

fix getClassPkId

Closes #11

See merge request !11
parents 9a861ab5 8435ad9b
......@@ -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,31 @@ public class ClassPKServiceImpl implements ClassPKService {
}
@Override
@Transactional
public Long getClassPkId(String classname) {
return classPkRepository.getClassPkId(classname);
Long id = classPkRepository.getClassPkId(classname);
if (id == null) {
try {
Class<?> aClass = Class.forName(classname);
return getClassPk(aClass).getId();
} catch (ClassNotFoundException e) {
return null;
}
} else {
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);
}
}
......@@ -48,4 +48,34 @@ public class ClassPKServiceTest extends ServiceTest {
assertThat(cpk2.getClassname(), equalTo(Long.class.getName()));
assertThat(cpk.getId(), equalTo(cpk2.getId()));
}
@Test
public void testGetClassPkId() {
final ClassPK cpk = classPkService.getClassPk(Long.class);
assertThat(cpk.getId(), not(nullValue()));
assertThat(cpk.getClassname(), equalTo(Long.class.getName()));
final Long id = classPkService.getClassPkId(Long.class.getName());
assertThat(id, equalTo(cpk.getId()));
}
@Test
public void testCreateClassPkId() {
final Long id = classPkService.getClassPkId(Long.class.getName());
assertThat(id, not(nullValue()));
final ClassPK cpk = classPkService.getClassPk(Long.class);
assertThat(id, equalTo(cpk.getId()));
assertThat(cpk.getId(), not(nullValue()));
assertThat(cpk.getClassname(), equalTo(Long.class.getName()));
}
/**
* Test that classPk is not created for classnames that don't exist
*/
@Test
public void testNoCreateClassPkForCNFE() {
final Long id = classPkService.getClassPkId("this.class.does.not.Exist");
assertThat(id, nullValue());
}
}
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