Search in sources :

Example 1 with ResourceFieldType

use of io.crnk.core.engine.information.resource.ResourceFieldType in project crnk-framework by crnk-project.

the class ResourceInformationProviderBase method buildResourceField.

protected void buildResourceField(BeanInformation beanDesc, BeanAttributeInformation attributeDesc, InformationBuilder.Field fieldBuilder) {
    fieldBuilder.underlyingName(attributeDesc.getName());
    fieldBuilder.jsonName(getJsonName(attributeDesc));
    ResourceFieldType fieldType = getFieldType(attributeDesc);
    fieldBuilder.fieldType(fieldType);
    fieldBuilder.access(getAccess(attributeDesc, fieldType));
    fieldBuilder.serializeType(getSerializeType(attributeDesc, fieldType));
    fieldBuilder.lookupIncludeBehavior(getLookupIncludeBehavior(attributeDesc));
    fieldBuilder.relationshipRepositoryBehavior(getRelationshipRepositoryBehavior(attributeDesc));
    Type genericType;
    if (useFieldType(attributeDesc)) {
        fieldBuilder.type(attributeDesc.getField().getType());
        genericType = attributeDesc.getField().getGenericType();
    } else {
        fieldBuilder.type(attributeDesc.getGetter().getReturnType());
        genericType = attributeDesc.getGetter().getGenericReturnType();
    }
    fieldBuilder.genericType(genericType);
    if (fieldType == ResourceFieldType.RELATIONSHIP) {
        fieldBuilder.oppositeResourceType(getResourceType(genericType, context));
        fieldBuilder.oppositeName(getOppositeName(attributeDesc));
        Optional<JsonApiRelation> relationAnnotation = attributeDesc.getAnnotation(JsonApiRelation.class);
        if (relationAnnotation.isPresent()) {
            boolean multiValued = Collection.class.isAssignableFrom(attributeDesc.getImplementationClass());
            String suffix = multiValued ? "Ids" : "Id";
            String idFieldName;
            if (relationAnnotation.get().idField().length() > 0) {
                idFieldName = relationAnnotation.get().idField();
            } else {
                idFieldName = attributeDesc.getName() + suffix;
            }
            BeanAttributeInformation idAttribute = beanDesc.getAttribute(idFieldName);
            if (idAttribute == null && multiValued && attributeDesc.getName().endsWith("s")) {
                // also try to correlate by removing ending s
                String idFieldNameTemp = attributeDesc.getName().substring(0, attributeDesc.getName().length() - 1) + suffix;
                BeanAttributeInformation idAttributeTemp = beanDesc.getAttribute(idFieldNameTemp);
                // make sure there are no custom get-named methods
                if (idAttributeTemp != null && attributeDesc.getGetter() != null && idAttributeTemp.getGetter().getReturnType().equals(attributeDesc.getGetter().getReturnType())) {
                    idFieldName = idFieldNameTemp;
                    idAttribute = idAttributeTemp;
                }
            }
            if (idAttribute != null) {
                fieldBuilder.idName(idFieldName);
                fieldBuilder.idType(idAttribute.getImplementationClass());
            }
        }
    }
}
Also used : ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) SerializeType(io.crnk.core.resource.annotations.SerializeType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonApiRelation(io.crnk.core.resource.annotations.JsonApiRelation) BeanAttributeInformation(io.crnk.core.engine.information.bean.BeanAttributeInformation)

Example 2 with ResourceFieldType

use of io.crnk.core.engine.information.resource.ResourceFieldType 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;
            }
        });
    }
}
Also used : ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) CountryTranslationPK(io.crnk.jpa.model.CountryTranslationPK) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess) LangEntity(io.crnk.jpa.model.LangEntity) JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) ResourceFieldAccessor(io.crnk.core.engine.information.resource.ResourceFieldAccessor) CountryTranslationEntity(io.crnk.jpa.model.CountryTranslationEntity) EntityManager(javax.persistence.EntityManager) List(java.util.List) CountryEntity(io.crnk.jpa.model.CountryEntity)

Aggregations

ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)2 BeanAttributeInformation (io.crnk.core.engine.information.bean.BeanAttributeInformation)1 ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)1 ResourceFieldAccessor (io.crnk.core.engine.information.resource.ResourceFieldAccessor)1 ResourceFieldImpl (io.crnk.core.engine.internal.information.resource.ResourceFieldImpl)1 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)1 JsonApiRelation (io.crnk.core.resource.annotations.JsonApiRelation)1 SerializeType (io.crnk.core.resource.annotations.SerializeType)1 JpaResourceInformationProvider (io.crnk.jpa.internal.JpaResourceInformationProvider)1 CountryEntity (io.crnk.jpa.model.CountryEntity)1 CountryTranslationEntity (io.crnk.jpa.model.CountryTranslationEntity)1 CountryTranslationPK (io.crnk.jpa.model.CountryTranslationPK)1 LangEntity (io.crnk.jpa.model.LangEntity)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1