use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.
the class HistoryFieldContributor method getResourceFields.
@Override
public List<ResourceField> getResourceFields(ResourceFieldContributorContext context) {
// this method could be omitted if the history field is added regularly to Project and Task resource. This would be
// simpler and recommended, but may not always be possible. Here we demonstrate doing it dynamically.
InformationBuilder.Field fieldBuilder = context.getInformationBuilder().createResourceField();
fieldBuilder.name("history");
fieldBuilder.genericType(new TypeToken<List<History>>() {
}.getType());
fieldBuilder.oppositeResourceType("history");
fieldBuilder.fieldType(ResourceFieldType.RELATIONSHIP);
// field values are "null" on resource and we make use of automated lookup to the relationship repository
// instead:
fieldBuilder.lookupIncludeBehavior(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS);
fieldBuilder.accessor(new ResourceFieldAccessor() {
@Override
public Object getValue(Object resource) {
return null;
}
@Override
public void setValue(Object resource, Object fieldValue) {
}
});
return Arrays.asList(fieldBuilder.build());
}
use of io.crnk.core.engine.information.resource.ResourceFieldAccessor 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;
}
});
}
}
use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.
the class ApprovalRelationshipRepository method getResourceFields.
@Override
public List<ResourceField> getResourceFields(ResourceFieldContributorContext context) {
InformationBuilder.Field fieldBuilder = context.getInformationBuilder().createResourceField();
fieldBuilder.name(relationshipName);
fieldBuilder.oppositeResourceType(oppositeResourceType);
fieldBuilder.lookupIncludeBehavior(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS);
fieldBuilder.fieldType(ResourceFieldType.RELATIONSHIP);
fieldBuilder.access(new ResourceFieldAccess(false, false, false, false, false));
fieldBuilder.accessor(new ResourceFieldAccessor() {
@Override
public Object getValue(Object resource) {
return null;
}
@Override
public void setValue(Object resource, Object fieldValue) {
}
});
fieldBuilder.type(processInfoClass);
fieldBuilder.genericType(processInfoClass);
return Lists.newArrayList(fieldBuilder.build());
}
use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.
the class AbstractQuerySpecTest method setup.
@Before
public void setup() {
ResourceInformationProvider resourceInformationProvider = new DefaultResourceInformationProvider(new NullPropertiesProvider(), ImmutableList.of(new OffsetLimitPagingBehavior(), new CustomOffsetLimitPagingBehavior()), new DefaultResourceFieldInformationProvider(), new JacksonResourceFieldInformationProvider()) {
@Override
protected List<ResourceField> getResourceFields(Class<?> resourceClass) {
List<ResourceField> fields = super.getResourceFields(resourceClass);
if (resourceClass == Task.class) {
// add additional field that is not defined on the class
String name = "computedAttribute";
ResourceFieldAccess access = new ResourceFieldAccess(true, true, true, true, true);
InformationBuilder informationBuilder = new DefaultInformationBuilder(new TypeParser());
InformationBuilder.Field fieldBuilder = informationBuilder.createResourceField();
fieldBuilder.type(Integer.class);
fieldBuilder.jsonName(name);
fieldBuilder.underlyingName(name);
fieldBuilder.access(access);
fieldBuilder.accessor(new ResourceFieldAccessor() {
public Object getValue(Object resource) {
return 13;
}
public void setValue(Object resource, Object fieldValue) {
}
});
fields.add(fieldBuilder.build());
}
return fields;
}
};
SimpleModule testModule = new SimpleModule("test");
testModule.addResourceInformationProvider(resourceInformationProvider);
CrnkBoot boot = new CrnkBoot();
boot.setServiceUrlProvider(new ConstantServiceUrlProvider("http://127.0.0.1"));
boot.addModule(testModule);
boot.setServiceDiscovery(new ReflectionsServiceDiscovery(getResourceSearchPackage()));
boot.boot();
moduleRegistry = boot.getModuleRegistry();
querySpecConverter = new DefaultQuerySpecConverter(moduleRegistry);
resourceRegistry = boot.getResourceRegistry();
}
use of io.crnk.core.engine.information.resource.ResourceFieldAccessor in project crnk-framework by crnk-project.
the class DefaultInformationBuilderTest method resource.
@Test
public void resource() {
InformationBuilder.Resource resource = builder.createResource(Task.class, "tasks");
resource.superResourceType("superTask");
resource.resourceType("changedTasks");
resource.resourceClass(Project.class);
InformationBuilder.Field idField = resource.addField("id", ResourceFieldType.ID, String.class);
idField.serializeType(SerializeType.EAGER);
idField.access(new ResourceFieldAccess(true, true, true, false, false));
ResourceFieldAccessor accessor = Mockito.mock(ResourceFieldAccessor.class);
InformationBuilder.Field projectField = resource.addField("project", ResourceFieldType.RELATIONSHIP, Project.class);
projectField.serializeType(SerializeType.EAGER);
projectField.access(new ResourceFieldAccess(true, false, true, false, false));
projectField.oppositeName("tasks");
projectField.relationshipRepositoryBehavior(RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER);
projectField.lookupIncludeBehavior(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS);
projectField.accessor(accessor);
ResourceInformation info = resource.build();
Assert.assertEquals("changedTasks", info.getResourceType());
Assert.assertEquals(Project.class, info.getResourceClass());
Assert.assertEquals("superTask", info.getSuperResourceType());
ResourceField idInfo = info.findFieldByName("id");
Assert.assertEquals("id", idInfo.getUnderlyingName());
Assert.assertEquals(String.class, idInfo.getType());
Assert.assertFalse(idInfo.getAccess().isFilterable());
Assert.assertFalse(idInfo.getAccess().isSortable());
Assert.assertTrue(idInfo.getAccess().isPostable());
Assert.assertTrue(idInfo.getAccess().isPatchable());
Assert.assertEquals(SerializeType.EAGER, idInfo.getSerializeType());
Assert.assertFalse(idInfo.isCollection());
ResourceField projectInfo = info.findFieldByName("project");
Assert.assertEquals("project", projectInfo.getUnderlyingName());
Assert.assertEquals("tasks", projectInfo.getOppositeName());
Assert.assertEquals(LookupIncludeBehavior.AUTOMATICALLY_ALWAYS, projectInfo.getLookupIncludeAutomatically());
Assert.assertEquals(Project.class, projectInfo.getType());
Assert.assertSame(accessor, projectInfo.getAccessor());
Assert.assertFalse(projectInfo.getAccess().isFilterable());
Assert.assertFalse(projectInfo.getAccess().isSortable());
Assert.assertFalse(projectInfo.getAccess().isPostable());
Assert.assertTrue(projectInfo.getAccess().isPatchable());
Assert.assertEquals(SerializeType.EAGER, projectInfo.getSerializeType());
Assert.assertEquals(RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER, projectInfo.getRelationshipRepositoryBehavior());
Assert.assertFalse(projectInfo.isCollection());
}
Aggregations