use of io.crnk.jpa.model.CountryTranslationEntity in project crnk-framework by crnk-project.
the class CustomResourceFieldTest method setup.
@Override
@Before
public void setup() {
super.setup();
SpringTransactionRunner transactionRunner = context.getBean(SpringTransactionRunner.class);
transactionRunner.doInTransaction(new Callable<Object>() {
@Override
public Object call() throws Exception {
EntityManager em = context.getBean(EntityManagerProducer.class).getEntityManager();
AbstractJpaTest.clear(em);
LangEntity en = new LangEntity();
en.setLangCd("en");
em.persist(en);
LangEntity de = new LangEntity();
de.setLangCd("de");
em.persist(de);
CountryEntity ch = new CountryEntity();
ch.setCountryCd("ch");
ch.setCtlActCd(true);
em.persist(ch);
CountryTranslationEntity chDe = new CountryTranslationEntity();
CountryTranslationPK chDePk = new CountryTranslationPK();
chDePk.setCountry(ch);
chDePk.setLang(de);
chDe.setCountryTranslationPk(chDePk);
chDe.setTxt("Schweiz");
em.persist(chDe);
CountryTranslationEntity chEn = new CountryTranslationEntity();
CountryTranslationPK chEnPk = new CountryTranslationPK();
chEnPk.setCountry(ch);
chEnPk.setLang(en);
chEn.setCountryTranslationPk(chEnPk);
chEn.setTxt("Switzerland");
em.persist(chEn);
em.flush();
return null;
}
});
}
use of io.crnk.jpa.model.CountryTranslationEntity in project crnk-framework by crnk-project.
the class CustomResourceFieldTest method setupModule.
@Override
protected void setupModule(final JpaModule module, boolean server) {
super.setupModule(module, server);
if (server) {
module.setResourceInformationProvider(new JpaResourceInformationProvider(new NullPropertiesProvider()) {
@Override
protected List<ResourceField> getResourceFields(Class clazz) {
List<ResourceField> fields = super.getResourceFields(clazz);
if (clazz == CountryEntity.class) {
List<String> languages = Arrays.asList("en", "de");
for (final String language : languages) {
ResourceFieldType resourceFieldType = ResourceFieldType.ATTRIBUTE;
String name = language + "Text";
Class<?> type = String.class;
ResourceFieldAccess access = new ResourceFieldAccess(true, true, true, false, false);
ResourceFieldImpl field = new ResourceFieldImpl(name, name, resourceFieldType, type, type, null, null, SerializeType.LAZY, LookupIncludeBehavior.NONE, access, null, null, null, RelationshipRepositoryBehavior.DEFAULT);
field.setAccessor(new ResourceFieldAccessor() {
@Override
public String getValue(Object resource) {
CountryEntity country = (CountryEntity) resource;
List<CountryTranslationEntity> translations = country.getTranslations();
CountryTranslationEntity translation = getTranslation(translations, language);
return translation != null ? translation.getTxt() : null;
}
@Override
public void setValue(Object resource, Object fieldValue) {
CountryEntity country = (CountryEntity) resource;
List<CountryTranslationEntity> translations = country.getTranslations();
CountryTranslationEntity translation = getTranslation(translations, language);
if (translation == null) {
EntityManager em = module.getEntityManager();
LangEntity langEntity = em.find(LangEntity.class, language);
if (langEntity == null) {
throw new IllegalStateException("language not found: " + language);
}
translation = new CountryTranslationEntity();
CountryTranslationPK pk = new CountryTranslationPK();
pk.setCountry(country);
pk.setLang(langEntity);
translation.setCountryTranslationPk(pk);
translations.add(translation);
}
translation.setTxt((String) fieldValue);
}
private CountryTranslationEntity getTranslation(List<CountryTranslationEntity> translations, String language) {
for (CountryTranslationEntity translation : translations) {
CountryTranslationPK translationPk = translation.getCountryTranslationPk();
String langCd = translationPk.getLang().getLangCd();
if (langCd.equals(language)) {
return translation;
}
}
return null;
}
});
fields.add(field);
}
}
return fields;
}
});
}
}
Aggregations