use of io.crnk.core.engine.information.resource.ResourceFieldAccess in project crnk-framework by crnk-project.
the class ResourceFieldAccessTest method and.
@Test
public void and() {
ResourceFieldAccess all = new ResourceFieldAccess(true, true, true, true, true);
ResourceFieldAccess none = new ResourceFieldAccess(false, false, false, false, false);
ResourceFieldAccess a1 = new ResourceFieldAccess(false, true, false, false, false);
ResourceFieldAccess a2 = new ResourceFieldAccess(false, false, true, false, false);
ResourceFieldAccess a3 = new ResourceFieldAccess(false, false, false, true, false);
ResourceFieldAccess a4 = new ResourceFieldAccess(false, false, false, false, true);
ResourceFieldAccess a5 = new ResourceFieldAccess(true, false, false, false, false);
Assert.assertEquals(a1, a1.and(all));
Assert.assertEquals(a1, all.and(a1));
Assert.assertEquals(none, none.and(a1));
Assert.assertEquals(a2, a2.and(all));
Assert.assertEquals(a2, all.and(a2));
Assert.assertEquals(none, none.and(a2));
Assert.assertEquals(a3, a3.and(all));
Assert.assertEquals(a3, all.and(a3));
Assert.assertEquals(none, none.and(a3));
Assert.assertEquals(a4, a4.and(all));
Assert.assertEquals(a4, all.and(a4));
Assert.assertEquals(none, none.and(a4));
Assert.assertEquals(none, none.and(all));
Assert.assertEquals(none, none.and(none));
Assert.assertEquals(all, all.and(all));
Assert.assertEquals(a5, a5.and(all));
Assert.assertEquals(a5, all.and(a5));
Assert.assertEquals(none, none.and(a5));
}
use of io.crnk.core.engine.information.resource.ResourceFieldAccess in project crnk-framework by crnk-project.
the class DefaultRegistryEntryBuilder method setupImplicitRelationshipRepository.
private ResponseRelationshipEntry setupImplicitRelationshipRepository(ResourceField relationshipField) {
RelationshipRepositoryBehavior behavior = relationshipField.getRelationshipRepositoryBehavior();
if (behavior == RelationshipRepositoryBehavior.DEFAULT) {
if (relationshipField.hasIdField() || relationshipField.getLookupIncludeAutomatically() == LookupIncludeBehavior.NONE) {
behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
} else {
behavior = RelationshipRepositoryBehavior.CUSTOM;
}
}
if (behavior == RelationshipRepositoryBehavior.IMPLICIT_FROM_OWNER) {
behavior = RelationshipRepositoryBehavior.FORWARD_OWNER;
}
if (behavior == RelationshipRepositoryBehavior.IMPLICIT_GET_OPPOSITE_MODIFY_OWNER) {
behavior = RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER;
}
if (behavior != RelationshipRepositoryBehavior.CUSTOM) {
ResourceInformation sourceInformation = relationshipField.getParentResourceInformation();
ResourceFieldAccess fieldAccess = relationshipField.getAccess();
RepositoryMethodAccess access = new RepositoryMethodAccess(fieldAccess.isPostable(), fieldAccess.isPatchable(), fieldAccess.isReadable(), fieldAccess.isPatchable());
RelationshipMatcher matcher = new RelationshipMatcher().rule().field(relationshipField).add();
RelationshipRepositoryInformationImpl implicitRepoInformation = new RelationshipRepositoryInformationImpl(matcher, access);
ForwardingRelationshipRepository repository;
if (behavior == RelationshipRepositoryBehavior.FORWARD_OWNER) {
repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OWNER, ForwardingDirection.OWNER);
} else if (behavior == RelationshipRepositoryBehavior.FORWARD_GET_OPPOSITE_SET_OWNER) {
repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OWNER);
} else {
PreconditionUtil.assertEquals("unknown behavior", RelationshipRepositoryBehavior.FORWARD_OPPOSITE, behavior);
repository = new ForwardingRelationshipRepository(sourceInformation.getResourceType(), matcher, ForwardingDirection.OPPOSITE, ForwardingDirection.OPPOSITE);
}
repository.setResourceRegistry(moduleRegistry.getResourceRegistry());
return setupRelationship(relationshipField, implicitRepoInformation, repository);
} else {
return null;
}
}
use of io.crnk.core.engine.information.resource.ResourceFieldAccess 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.ResourceFieldAccess 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.ResourceFieldAccess 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();
}
Aggregations