All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.andrewribeiro.ribrest.model.interfaces.Model Maven / Gradle / Ivy

Go to download

Ribrest Framework - A simple Java framework that truly improve your productivity when developing restful based webservices.

There is a newer version: 1.27.0
Show newest version
package br.com.andrewribeiro.ribrest.model.interfaces;

import br.com.andrewribeiro.ribrest.annotations.RibrestIgnoreField;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

/**
 *
 * @author Andrew Ribeiro
 */
public interface Model {

    public final static String PERSISTED_MODEL_KEY = UUID.randomUUID().toString();

    default public void merge(Model modelToMerge) {
        List thisInstanceAttributes = this.getAllAttributesExceptsId();
        List modelToMergeAttributes = modelToMerge.getAllAttributesExceptsId();
        IntStream.range(0, thisInstanceAttributes.size()).forEach((index) -> {
            Field thisIntanceField = thisInstanceAttributes.get(index);
            Field modelToMergeField = modelToMergeAttributes.get(index);
            thisIntanceField.setAccessible(true);
            modelToMergeField.setAccessible(true);
            try {
                Object attributeInstance = modelToMergeField.get(modelToMerge);
                attributeInstance = attributeInstance == null ? thisIntanceField.get(this) : attributeInstance;
                if (modelToMergeField.isAnnotationPresent(OneToOne.class)) {
                    attributeInstance = ((Model) attributeInstance).getId() == null ? thisIntanceField.get(this) : attributeInstance;
                }
                thisIntanceField.set(this, attributeInstance);
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                throw new RuntimeException(ex.getCause());
            }
        });
    }

    default public List getAllAttributes() {
        Class thisClass = this.getClass();
        List thisClassFields = new ArrayList();
        for (; thisClass != null; thisClass = thisClass.getSuperclass()) {
            Field[] fields = thisClass.getDeclaredFields();
            for (Field field : fields) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    thisClassFields.add(field);
                }
            }
        }
        return thisClassFields;
    }

    default public List getAllAttributesExceptsId() {
        return getAllAttributes().stream()
                .filter(attribute -> !attribute.isAnnotationPresent(Id.class))
                .collect(Collectors.toList());
    }

    default public List getAllAttributesExceptsBidirectionalModels() {
        return getAllAttributes().stream()
                .filter(attribute -> {
                    return !(attribute.isAnnotationPresent(OneToOne.class) && !attribute.getAnnotation(OneToOne.class).mappedBy().isEmpty());
                })
                .collect(Collectors.toList());
    }

    default public List getAllModelAttributes() {
        return getAllAttributes().stream()
                .filter(attribute -> Model.class.isAssignableFrom(attribute.getType()))
                .collect(Collectors.toList());
    }

    default public List getAllModelNonBidirectionalAttributes() {
        return getAllModelAttributes().stream().filter(attribute -> !(attribute.isAnnotationPresent(OneToOne.class) && "".equals(attribute.getAnnotation(OneToOne.class).mappedBy())))
                .collect(Collectors.toList());
    }
    
    default public List getAllCollectionModelAttributes(){
        return getAllAttributes().stream()
                .filter(attribute -> attribute.isAnnotationPresent(OneToMany.class))
                .collect(Collectors.toList());
    }
    
    default public List getAllInverseCollectionModelAttributes(){
        return getAllAttributes().stream()
                .filter(attribute -> attribute.isAnnotationPresent(ManyToOne.class))
                .collect(Collectors.toList());
    }

    default public List getIgnoredAttributes() {

        List ignoredFields, allFields;
        ignoredFields = new ArrayList();
        allFields = getAllAttributes();
        allFields.forEach((field) -> {
            if (field.isAnnotationPresent(RibrestIgnoreField.class)) {
                ignoredFields.add(field);
            }
        });
        return ignoredFields;
    }

    public Long getId();

    public void setId(Long id);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy