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

io.mateu.mdd.vaadin.views.FieldToViewMapper Maven / Gradle / Ivy

There is a newer version: 1.1.60
Show newest version
package io.mateu.mdd.vaadin.views;

import com.vaadin.data.Binder;
import io.mateu.mdd.core.ui.MDDUIAccessor;
import io.mateu.mdd.shared.annotations.TextArea;
import io.mateu.mdd.shared.interfaces.App;
import io.mateu.mdd.shared.interfaces.RpcView;
import io.mateu.mdd.vaadin.components.app.views.firstLevel.FakeComponent;
import io.mateu.mdd.vaadin.components.views.*;
import io.mateu.mdd.vaadin.controllers.VoidController;
import io.mateu.mdd.vaadin.controllers.secondLevel.EditorController;
import io.mateu.mdd.vaadin.controllers.secondLevel.ListViewController;
import io.mateu.mdd.vaadin.controllers.thirdLevel.FieldController;
import io.mateu.mdd.vaadin.navigation.View;
import io.mateu.mdd.vaadin.navigation.ViewStack;
import io.mateu.mdd.vaadin.pojos.Error;
import io.mateu.mdd.vaadin.pojos.ModelField;
import io.mateu.reflection.ReflectionHelper;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import java.util.Collection;

public class FieldToViewMapper {

    private final ViewStack stack;
    private final App app;

    public FieldToViewMapper(ViewStack stack) {
        this.stack = stack;
        this.app = MDDUIAccessor.getApp();
    }

    //todo: open for extension, close for modification
    public View toView(ModelField modelField, String step) {
        if (modelField == null) {
            return new BrokenLinkView(stack);
        }

        if (RpcView.class.isAssignableFrom(modelField.getField().getType())) {
            try {
                RpcListViewComponent component =
                        new RpcListViewComponent((RpcView) ReflectionHelper.getValue(modelField.getField(), modelField.getInstance()));
                component.buildIfNeeded();
                View view = new View(stack, component, new ListViewController(component));
                return view;
            } catch (Exception e) {
                return new ProblemView(stack, "Error", new Error(e));
            }
        }


        if (Collection.class.isAssignableFrom(modelField.getField().getType()) && (
                modelField.getField().isAnnotationPresent(OneToMany.class)
                || modelField.getField().isAnnotationPresent(ManyToMany.class)
                )) {
            try {
                EditorViewComponent currentEditor = (EditorViewComponent) stack.getLast().getViewComponent();

                return new View(stack, new JPACollectionFieldViewComponent(modelField.getField().getGenericClass(), modelField.getField(), currentEditor, true), new VoidController());
            } catch (Exception e) {
                return new ProblemView(stack, "Error", new Error(e));
            }
        }


        if (Collection.class.isAssignableFrom(modelField.getField().getType())) {
            try {
                EditorViewComponent currentEditor = (EditorViewComponent) stack.getLast().getViewComponent();

                return new View(stack, new OwnedCollectionViewComponent(currentEditor.getBinder(),
                        modelField.getField(), (Integer) MDDUIAccessor.getPendingResult()), new VoidController());
            } catch (Exception e) {
                return new ProblemView(stack, "Error", new Error(e));
            }
        }

        if (modelField.getField().isAnnotationPresent(TextArea.class)) {
            try {
                EditorViewComponent currentEditor = (EditorViewComponent) stack.getLast().getViewComponent();

                EditorViewComponent refEditorViewComponent = new TextAreaViewComponent(currentEditor.getBinder(), modelField.getField()
                            , ((Binder.Binding)currentEditor.getBinder().getBinding(modelField.getField().getId()).get()).getField());
                return new View(stack, refEditorViewComponent,
                        new EditorController(ReflectionHelper.getValue(modelField.getField(), currentEditor.getModel())));
            } catch (Exception e) {
                return new ProblemView(stack, "Error", new Error(e));
            }
        }

        if (!ReflectionHelper.isBasico(modelField.getField().getType())) {
            try {
                EditorViewComponent currentEditor = (EditorViewComponent) stack.getLast().getViewComponent();

                EditorViewComponent refEditorViewComponent = null;
                if (modelField.getField().getType().isAnnotationPresent(Entity.class)) {
                    refEditorViewComponent = new ReferencedEntityViewComponent(currentEditor.getBinder(), modelField.getField());
                } else if (Object.class.equals(modelField.getField().getType())) {
                    refEditorViewComponent = new TextAreaViewComponent(currentEditor.getBinder(), modelField.getField()
                    , ((Binder.Binding)currentEditor.getBinder().getBinding(modelField.getField().getId()).get()).getField());
                } else {
                    refEditorViewComponent = new OwnedPojoViewComponent(currentEditor.getBinder(), modelField.getField());
                }

                return new View(stack, refEditorViewComponent,
                        new EditorController(ReflectionHelper.getValue(modelField.getField(), currentEditor.getModel())));
            } catch (Exception e) {
                return new ProblemView(stack, "Error", new Error(e));
            }
        }


        ComponentView view = new ComponentView(stack, modelField.getField().getName(), null,
                new FakeComponent("Field " + modelField.getField().getName()));
        view.setController(new FieldController(modelField));
        return view;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy