<commit_before>/*
 * This file is part of ###PROJECT_NAME###
 *
 * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
 *                    Desenvolvemento Tecnolóxico de Galicia
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.navalplanner.web.orders;

import static org.navalplanner.web.I18nHelper._;

import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.LogFactory;
import org.hibernate.validator.InvalidValue;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.orders.entities.HoursGroup;
import org.navalplanner.business.orders.entities.Order;
import org.navalplanner.business.orders.entities.OrderElement;
import org.navalplanner.business.orders.entities.OrderLine;
import org.navalplanner.business.orders.entities.OrderLineGroup;
import org.navalplanner.web.common.IMessagesForUser;
import org.navalplanner.web.common.Level;
import org.navalplanner.web.common.MessagesForUser;
import org.navalplanner.web.common.OnlyOneVisible;
import org.navalplanner.web.common.Util;
import org.navalplanner.web.orders.materials.AssignedMaterialsToOrderElementController;
import org.navalplanner.web.orders.materials.OrderElementMaterialAssignmentsComponent;
import org.navalplanner.web.planner.order.IOrderPlanningGate;
import org.navalplanner.web.templates.IOrderTemplatesControllerEntryPoints;
import org.navalplanner.web.tree.TreeComponent;
import org.navalplanner.web.users.OrderAuthorizationController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.ComboitemRenderer;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabbox;
import org.zkoss.zul.api.Window;

/**
 * Controller for CRUD actions <br />
 * @author <NAME> <<EMAIL>>
 */
@org.springframework.stereotype.Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class OrderCRUDController extends GenericForwardComposer {

    private final class LabelCreatorForInvalidValues implements
            IMessagesForUser.ICustomLabelCreator {
        @Override
        public Component createLabelFor(
                InvalidValue invalidValue) {
            if (invalidValue.getBean() instanceof OrderElement) {
                Label result = new Label();

                String orderElementName;
                if (invalidValue.getBean() instanceof Order) {
                    orderElementName = _("Order");
                } else {
                    orderElementName = ((OrderElement) invalidValue
                            .getBean()).getName();
                }

                result.setValue(orderElementName + " "
                        + invalidValue.getPropertyName() + ": "
                        + invalidValue.getMessage());
                return result;
            } else if (invalidValue.getBean() instanceof HoursGroup) {
                Label result = new Label();
                HoursGroup hoursGroup = (HoursGroup) invalidValue.getBean();
                OrderLine parentOrderLine = hoursGroup.getParentOrderLine();
                result.setValue(_("Hours Group at ")
                        + parentOrderLine.getName() + ". "
                        + invalidValue.getPropertyName() + ": "
                        + invalidValue.getMessage());
                return result;
            }else {
                return MessagesForUser.createLabelFor(invalidValue);
            }
        }
    }

    private static final org.apache.commons.logging.Log LOG = LogFactory
            .getLog(OrderCRUDController.class);

    @Autowired
    private IOrderModel orderModel;

    private IMessagesForUser messagesForUser;

    private Component messagesContainer;

    @Resource
    private IOrderTemplatesControllerEntryPoints orderTemplates;

    private Window editWindow;

    private Window listWindow;

    private Tabbox tabboxOrder;

    private Tab selectedTab;

    private OnlyOneVisible cachedOnlyOneVisible;

    private IOrderPlanningGate planningControllerEntryPoints;

    private BaseCalendarsComboitemRenderer baseCalendarsComboitemRenderer = new BaseCalendarsComboitemRenderer();

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        messagesForUser = new MessagesForUser(messagesContainer);
        comp.setVariable("controller", this, true);
    }

    private void addEditWindowIfNeeded() {
        if (editWindow != null) {
            return;
        }
        Map<String, Object> editWindowArgs = new HashMap<String, Object>();
        editWindowArgs.put("top_id", editWindowArgs);
        Component parent = listWindow.getParent();
        listWindow.setVisible(false);
        cachedOnlyOneVisible = null;
        editWindow = (Window) Executions.createComponents(
                "/orders/_edition.zul",
                parent, editWindowArgs);
        Map<String, Object> editOrderElementArgs = new HashMap<String, Object>();
        editOrderElementArgs.put("top_id", "editOrderElement");
        Component editOrderElement = Executions.createComponents(
                "/orders/_editOrderElement.zul",
                parent, editOrderElementArgs);
        try {
            setupEditControllers();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Util.createBindingsFor(editWindow);
        Util.reloadBindings(editWindow);
        Util.createBindingsFor(editOrderElement);
        Util.reloadBindings(editOrderElement);
    }

    private void setupEditControllers() throws Exception {
        Component comp = self;
        OrderElementController orderElementController = new OrderElementController();
        orderElementController.doAfterCompose(comp
                .getFellow("editOrderElement"));

        setupOrderElementTreeController(comp, orderElementController);
        setupAsignedHoursToOrderElementController(comp);
        setupManageOrderElementAdvancesController(comp);
        setupAssignedLabelsToOrderElementController(comp);
        setupAssignedCriterionRequirementsToOrderElementController(comp);
        setupAssignedMaterialsToOrderElementController(comp);
        setupAssignedTaskQualityFormsToOrderElementController(comp);
        setupOrderAuthorizationController(comp);
    }

    private void setupOrderElementTreeController(Component comp,
            OrderElementController orderElementController) throws Exception {
        TreeComponent orderElementsTree = (TreeComponent) editWindow
                .getFellow("orderElementTree");
        orderElementsTree.useController(new OrderElementTreeController(
                orderModel, orderElementController));
    }

    private IOrderElementModel getOrderElementModel() {
        final Order order = (Order) orderModel.getOrder();
        return orderModel.getOrderElementModel(order);
    }

    private AsignedHoursToOrderElementController assignedHoursController;

    private void setupAsignedHoursToOrderElementController(Component comp) throws Exception{
        Component orderElementHours = editWindow.getFellowIfAny("orderElementHours");
        assignedHoursController = (AsignedHoursToOrderElementController)
            orderElementHours.getVariable("asignedHoursToOrderElementController", true);
    }

    private ManageOrderElementAdvancesController manageOrderElementAdvancesController;

    private void setupManageOrderElementAdvancesController(Component comp) throws Exception {
        Component orderElementAdvances = editWindow.getFellowIfAny("orderElementAdvances");
        manageOrderElementAdvancesController = (ManageOrderElementAdvancesController)
            orderElementAdvances.getVariable("manageOrderElementAdvancesController", true);
    }

    private AssignedLabelsToOrderElementController assignedLabelsController;

    private void setupAssignedLabelsToOrderElementController(Component comp)
    throws Exception {
        Component orderElementLabels = editWindow.getFellowIfAny("orderElementLabels");
        assignedLabelsController = (AssignedLabelsToOrderElementController)
            orderElementLabels.getVariable("assignedLabelsController", true);
    }

    private AssignedCriterionRequirementToOrderElementController assignedCriterionRequirementController;

    private void setupAssignedCriterionRequirementsToOrderElementController(
            Component comp) throws Exception {
        Component orderElementCriterionRequirements = editWindow
                .getFellowIfAny("orderElementCriterionRequirements");
        assignedCriterionRequirementController = (AssignedCriterionRequirementToOrderElementController) orderElementCriterionRequirements
                .getVariable("assignedCriterionRequirementController", true);
    }

    private AssignedMaterialsToOrderElementController assignedMaterialsController;

    private void setupAssignedMaterialsToOrderElementController(
            Component comp) throws Exception {
        OrderElementMaterialAssignmentsComponent assignmentsComponent = (OrderElementMaterialAssignmentsComponent) editWindow
                .getFellowIfAny("orderElementMaterials");
        assignedMaterialsController = assignmentsComponent.getController();
    }

    private AssignedTaskQualityFormsToOrderElementController assignedTaskQualityFormController;

    private void setupAssignedTaskQualityFormsToOrderElementController(
            Component comp) throws Exception {
        Component orderElementTaskQualityForms = editWindow
                .getFellowIfAny("orderElementTaskQualityForms");
        assignedTaskQualityFormController = (AssignedTaskQualityFormsToOrderElementController) orderElementTaskQualityForms
                .getVariable("assignedTaskQualityFormsController", true);
    }
    
    private OrderAuthorizationController orderAuthorizationController;

    private void setupOrderAuthorizationController(
            Component comp) {
        Component orderElementAuthorizations = editWindow
            .getFellowIfAny("orderElementAuthorizations");
        orderAuthorizationController = (OrderAuthorizationController) orderElementAuthorizations
            .getVariable("orderAuthorizationController", true);
        orderAuthorizationController.setMessagesForUserComponent(messagesForUser);
    }

    public List<Order> getOrders() {
        return orderModel.getOrders();
    }

    private OnlyOneVisible getVisibility() {
        if (cachedOnlyOneVisible == null) {
            cachedOnlyOneVisible = new OnlyOneVisible(listWindow);
        }
        return cachedOnlyOneVisible;
    }

    public OrderLineGroup getOrder() {
        return orderModel.getOrder();
    }

    public void saveAndContinue() {
        setCurrentTab();
        final boolean couldSave = save();
        if (couldSave) {
            selectTab(getCurrentTab().getId());
            orderModel.initEdit((Order) orderModel.getOrder());
            orderAuthorizationController.setOrder((Order) orderModel.getOrder());
            initializeTabs();
            showWindow(editWindow);
        }
    }

    public void saveAndExit() {
        final boolean couldSave = save();
        if (couldSave) {
            goToList();
        }
    }

    private boolean save() {

        if (!manageOrderElementAdvancesController.save()) {
            selectTab("tabAdvances");
        }
        if (!assignedCriterionRequirementController.close()) {
            selectTab("tabRequirements");
        }
        selectTab("tabTaskQualityForm");
        if (!assignedTaskQualityFormController.confirm()) {
            return false;
        }
        try {
            orderModel.save();
            orderAuthorizationController.save();
            messagesForUser.showMessage(Level.INFO, _("Order saved"));
            return true;
        } catch (ValidationException e) {
            messagesForUser.showInvalidValues(e, new LabelCreatorForInvalidValues());
        }
        return false;
    }

    private void setCurrentTab() {
        Tabbox tabboxOrder = (Tabbox) editWindow.getFellowIfAny("tabboxOrder");
        if (tabboxOrder != null) {
            selectedTab = tabboxOrder.getSelectedTab();
        }
    }

    private Tab getCurrentTab() {
        return selectedTab;
    }

    private void selectTab(String str) {
        Tab tab = (Tab) editWindow.getFellowIfAny(str);
        if (tab != null) {
            tab.setSelected(true);
        }
    }

    public void goToList() {
        showWindow(listWindow);
    }

    public void reloadHoursGroupOrder() {
        assignedCriterionRequirementController
                .openWindow(getOrderElementModel());
        assignedCriterionRequirementController.reload();
    }

    private void showWindow(Window window) {
        getVisibility().showOnly(window);
        Util.reloadBindings(window);
    }

    public void cancel() {
        goToList();
    }

    public void up() {
        if (onUp == null) {
            throw new IllegalStateException(
                    "in order to call up onUp action should have been set");
        }
        onUp.run();
    }

    public void confirmRemove(Order order) {
        try {
            int status = Messagebox.show(_("Confirm deleting {0}. Are you sure?", order.getName()), "Delete",
                    Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION);
            if (Messagebox.OK == status) {
                remove(order);
            }
        } catch (InterruptedException e) {
            messagesForUser.showMessage(
                    Level.ERROR, e.getMessage());
            LOG.error(_("Error on showing removing element: ", order.getId()), e);
        }
    }

    private void remove(Order order) {
        orderModel.remove(order);
        Util.reloadBindings(self);
        messagesForUser.showMessage(Level.INFO, _("Removed {0}", order.getName()));
    }

    public void schedule(Order order) {
        if (order.isScheduled()) {
            planningControllerEntryPoints.goToScheduleOf(order);
        }else{
            try {
                Messagebox
                        .show(_("The order has no scheduled elements"),
                                _("Information"), Messagebox.OK,
                        Messagebox.INFORMATION);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void createTemplate(Order order) {
        orderTemplates.goToCreateTemplateFrom(order);
    }

    private Runnable onUp;

    public void goToEditForm(Order order) {
        planningControllerEntryPoints.goToOrderDetails(order);
        orderAuthorizationController.setOrder(order);
    }

    public void initEdit(Order order) {
        orderModel.initEdit(order);
        showEditWindow(_("Edit order"));
    }

    private void showEditWindow(String title) {
        addEditWindowIfNeeded();
        clearEditWindow();
        initializeTabs();
        editWindow.setTitle(title);
        showWindow(editWindow);
    }

    private void initializeTabs() {
        final IOrderElementModel orderElementModel = getOrderElementModel();

        assignedHoursController.openWindow(orderElementModel);
        manageOrderElementAdvancesController.openWindow(orderElementModel);
        assignedLabelsController.openWindow(orderElementModel);
        assignedCriterionRequirementController.openWindow(orderElementModel);
        assignedMaterialsController.openWindow(orderElementModel
                .getOrderElement());
        assignedTaskQualityFormController.openWindow(orderElementModel);
    }

    private void clearEditWindow() {<commit_msg>ItEr43S09ImplantacionAplicacionItEr42S13: [Bug #232] Fixing bug.<commit_after>/*
 * This file is part of ###PROJECT_NAME###
 *
 * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
 *                    Desenvolvemento Tecnolóxico de Galicia
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.navalplanner.web.orders;

import static org.navalplanner.web.I18nHelper._;

import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.LogFactory;
import org.hibernate.validator.InvalidValue;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.orders.entities.HoursGroup;
import org.navalplanner.business.orders.entities.Order;
import org.navalplanner.business.orders.entities.OrderElement;
import org.navalplanner.business.orders.entities.OrderLine;
import org.navalplanner.business.orders.entities.OrderLineGroup;
import org.navalplanner.web.common.IMessagesForUser;
import org.navalplanner.web.common.Level;
import org.navalplanner.web.common.MessagesForUser;
import org.navalplanner.web.common.OnlyOneVisible;
import org.navalplanner.web.common.Util;
import org.navalplanner.web.orders.materials.AssignedMaterialsToOrderElementController;
import org.navalplanner.web.orders.materials.OrderElementMaterialAssignmentsComponent;
import org.navalplanner.web.planner.order.IOrderPlanningGate;
import org.navalplanner.web.templates.IOrderTemplatesControllerEntryPoints;
import org.navalplanner.web.tree.TreeComponent;
import org.navalplanner.web.users.OrderAuthorizationController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.ComboitemRenderer;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabbox;
import org.zkoss.zul.api.Window;

/**
 * Controller for CRUD actions <br />
 * @author <NAME> <<EMAIL>>
 */
@org.springframework.stereotype.Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class OrderCRUDController extends GenericForwardComposer {

    private final class LabelCreatorForInvalidValues implements
            IMessagesForUser.ICustomLabelCreator {
        @Override
        public Component createLabelFor(
                InvalidValue invalidValue) {
            if (invalidValue.getBean() instanceof OrderElement) {
                Label result = new Label();

                String orderElementName;
                if (invalidValue.getBean() instanceof Order) {
                    orderElementName = _("Order");
                } else {
                    orderElementName = ((OrderElement) invalidValue
                            .getBean()).getName();
                }

                result.setValue(orderElementName + " "
                        + invalidValue.getPropertyName() + ": "
                        + invalidValue.getMessage());
                return result;
            } else if (invalidValue.getBean() instanceof HoursGroup) {
                Label result = new Label();
                HoursGroup hoursGroup = (HoursGroup) invalidValue.getBean();
                OrderLine parentOrderLine = hoursGroup.getParentOrderLine();
                result.setValue(_("Hours Group at ")
                        + parentOrderLine.getName() + ". "
                        + invalidValue.getPropertyName() + ": "
                        + invalidValue.getMessage());
                return result;
            }else {
                return MessagesForUser.createLabelFor(invalidValue);
            }
        }
    }

    private static final org.apache.commons.logging.Log LOG = LogFactory
            .getLog(OrderCRUDController.class);

    @Autowired
    private IOrderModel orderModel;

    private IMessagesForUser messagesForUser;

    private Component messagesContainer;

    @Resource
    private IOrderTemplatesControllerEntryPoints orderTemplates;

    private Window editWindow;

    private Window listWindow;

    private Tabbox tabboxOrder;

    private Tab selectedTab;

    private OnlyOneVisible cachedOnlyOneVisible;

    private IOrderPlanningGate planningControllerEntryPoints;

    private BaseCalendarsComboitemRenderer baseCalendarsComboitemRenderer = new BaseCalendarsComboitemRenderer();

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        messagesForUser = new MessagesForUser(messagesContainer);
        comp.setVariable("controller", this, true);
    }

    private void addEditWindowIfNeeded() {
        if (editWindow != null) {
            return;
        }
        Map<String, Object> editWindowArgs = new HashMap<String, Object>();
        editWindowArgs.put("top_id", editWindowArgs);
        Component parent = listWindow.getParent();
        listWindow.setVisible(false);
        cachedOnlyOneVisible = null;
        editWindow = (Window) Executions.createComponents(
                "/orders/_edition.zul",
                parent, editWindowArgs);
        Map<String, Object> editOrderElementArgs = new HashMap<String, Object>();
        editOrderElementArgs.put("top_id", "editOrderElement");
        Component editOrderElement = Executions.createComponents(
                "/orders/_editOrderElement.zul",
                parent, editOrderElementArgs);
        try {
            setupEditControllers();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Util.createBindingsFor(editWindow);
        Util.reloadBindings(editWindow);
        Util.createBindingsFor(editOrderElement);
        Util.reloadBindings(editOrderElement);
    }

    private void setupEditControllers() throws Exception {
        Component comp = self;
        OrderElementController orderElementController = new OrderElementController();
        orderElementController.doAfterCompose(comp
                .getFellow("editOrderElement"));

        setupOrderElementTreeController(comp, orderElementController);
        setupAsignedHoursToOrderElementController(comp);
        setupManageOrderElementAdvancesController(comp);
        setupAssignedLabelsToOrderElementController(comp);
        setupAssignedCriterionRequirementsToOrderElementController(comp);
        setupAssignedMaterialsToOrderElementController(comp);
        setupAssignedTaskQualityFormsToOrderElementController(comp);
        setupOrderAuthorizationController(comp);
    }

    private void setupOrderElementTreeController(Component comp,
            OrderElementController orderElementController) throws Exception {
        TreeComponent orderElementsTree = (TreeComponent) editWindow
                .getFellow("orderElementTree");
        orderElementsTree.useController(new OrderElementTreeController(
                orderModel, orderElementController));
    }

    private IOrderElementModel getOrderElementModel() {
        final Order order = (Order) orderModel.getOrder();
        return orderModel.getOrderElementModel(order);
    }

    private AsignedHoursToOrderElementController assignedHoursController;

    private void setupAsignedHoursToOrderElementController(Component comp) throws Exception{
        Component orderElementHours = editWindow.getFellowIfAny("orderElementHours");
        assignedHoursController = (AsignedHoursToOrderElementController)
            orderElementHours.getVariable("asignedHoursToOrderElementController", true);
    }

    private ManageOrderElementAdvancesController manageOrderElementAdvancesController;

    private void setupManageOrderElementAdvancesController(Component comp) throws Exception {
        Component orderElementAdvances = editWindow.getFellowIfAny("orderElementAdvances");
        manageOrderElementAdvancesController = (ManageOrderElementAdvancesController)
            orderElementAdvances.getVariable("manageOrderElementAdvancesController", true);
    }

    private AssignedLabelsToOrderElementController assignedLabelsController;

    private void setupAssignedLabelsToOrderElementController(Component comp)
    throws Exception {
        Component orderElementLabels = editWindow.getFellowIfAny("orderElementLabels");
        assignedLabelsController = (AssignedLabelsToOrderElementController)
            orderElementLabels.getVariable("assignedLabelsController", true);
    }

    private AssignedCriterionRequirementToOrderElementController assignedCriterionRequirementController;

    private void setupAssignedCriterionRequirementsToOrderElementController(
            Component comp) throws Exception {
        Component orderElementCriterionRequirements = editWindow
                .getFellowIfAny("orderElementCriterionRequirements");
        assignedCriterionRequirementController = (AssignedCriterionRequirementToOrderElementController) orderElementCriterionRequirements
                .getVariable("assignedCriterionRequirementController", true);
    }

    private AssignedMaterialsToOrderElementController assignedMaterialsController;

    private void setupAssignedMaterialsToOrderElementController(
            Component comp) throws Exception {
        OrderElementMaterialAssignmentsComponent assignmentsComponent = (OrderElementMaterialAssignmentsComponent) editWindow
                .getFellowIfAny("orderElementMaterials");
        assignedMaterialsController = assignmentsComponent.getController();
    }

    private AssignedTaskQualityFormsToOrderElementController assignedTaskQualityFormController;

    private void setupAssignedTaskQualityFormsToOrderElementController(
            Component comp) throws Exception {
        Component orderElementTaskQualityForms = editWindow
                .getFellowIfAny("orderElementTaskQualityForms");
        assignedTaskQualityFormController = (AssignedTaskQualityFormsToOrderElementController) orderElementTaskQualityForms
                .getVariable("assignedTaskQualityFormsController", true);
    }
    
    private OrderAuthorizationController orderAuthorizationController;

    private void setupOrderAuthorizationController(
            Component comp) {
        Component orderElementAuthorizations = editWindow
            .getFellowIfAny("orderElementAuthorizations");
        orderAuthorizationController = (OrderAuthorizationController) orderElementAuthorizations
            .getVariable("orderAuthorizationController", true);
        orderAuthorizationController.setMessagesForUserComponent(messagesForUser);
    }

    public List<Order> getOrders() {
        return orderModel.getOrders();
    }

    private OnlyOneVisible getVisibility() {
        if (cachedOnlyOneVisible == null) {
            cachedOnlyOneVisible = new OnlyOneVisible(listWindow);
        }
        return cachedOnlyOneVisible;
    }

    public OrderLineGroup getOrder() {
        return orderModel.getOrder();
    }

    public void saveAndContinue() {
        setCurrentTab();
        final boolean couldSave = save();
        if (couldSave) {
            selectTab(getCurrentTab().getId());
            orderModel.initEdit((Order) orderModel.getOrder());
            orderAuthorizationController.setOrder((Order) orderModel.getOrder());
            initializeTabs();
            showWindow(editWindow);
        }
    }

    public void saveAndExit() {
        final boolean couldSave = save();
        if (couldSave) {
            goToList();
        }
    }

    private boolean save() {

        if (!manageOrderElementAdvancesController.save()) {
            selectTab("tabAdvances");
        }
        if (!assignedCriterionRequirementController.close()) {
            selectTab("tabRequirements");
        }
        selectTab("tabTaskQualityForm");
        if (!assignedTaskQualityFormController.confirm()) {
            return false;
        }
        try {
            orderModel.save();
            orderAuthorizationController.save();
            messagesForUser.showMessage(Level.INFO, _("Order saved"));
            return true;
        } catch (ValidationException e) {
            messagesForUser.showInvalidValues(e, new LabelCreatorForInvalidValues());
        }
        return false;
    }

    private void setCurrentTab() {
        Tabbox tabboxOrder = (Tabbox) editWindow.getFellowIfAny("tabboxOrder");
        if (tabboxOrder != null) {
            selectedTab = tabboxOrder.getSelectedTab();
        }
    }

    private Tab getCurrentTab() {
        return selectedTab;
    }

    private void selectTab(String str) {
        Tab tab = (Tab) editWindow.getFellowIfAny(str);
        if (tab != null) {
            tab.setSelected(true);
        }
    }

    public void goToList() {
        showWindow(listWindow);
    }

    public void reloadHoursGroupOrder() {
        assignedCriterionRequirementController
                .openWindow(getOrderElementModel());
        assignedCriterionRequirementController.reload();
    }

    private void showWindow(Window window) {
        getVisibility().showOnly(window);
        Util.reloadBindings(window);
    }

    public void cancel() {
        goToList();
    }

    public void up() {
        if (onUp == null) {
            throw new IllegalStateException(
                    "in order to call up onUp action should have been set");
        }
        onUp.run();
    }

    public void confirmRemove(Order order) {
        try {
            int status = Messagebox.show(_("Confirm deleting {0}. Are you sure?", order.getName()), "Delete",
                    Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION);
            if (Messagebox.OK == status) {
                remove(order);
            }
        } catch (InterruptedException e) {
            messagesForUser.showMessage(
                    Level.ERROR, e.getMessage());
            LOG.error(_("Error on showing removing element: ", order.getId()), e);
        }
    }

    private void remove(Order order) {
        orderModel.remove(order);
        Util.reloadBindings(self);
        messagesForUser.showMessage(Level.INFO, _("Removed {0}", order.getName()));
    }

    public void schedule(Order order) {
        if (order.isScheduled()) {
            planningControllerEntryPoints.goToScheduleOf(order);
        }else{
            try {
                Messagebox
                        .show(_("The order has no scheduled elements"),
                                _("Information"), Messagebox.OK,
                        Messagebox.INFORMATION);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void createTemplate(Order order) {
        orderTemplates.goToCreateTemplateFrom(order);
    }

    private Runnable onUp;

    public void goToEditForm(Order order) {
        planningControllerEntryPoints.goToOrderDetails(order);
    }

    public void initEdit(Order order) {
        orderModel.initEdit(order);
        addEditWindowIfNeeded();
        orderAuthorizationController.setOrder(order);
        showEditWindow(_("Edit order"));
    }

    private void showEditWindow(String title) {
        addEditWindowIfNeeded();
        clearEditWindow();
        initializeTabs();
        editWindow.setTitle(title);
        showWindow(editWindow);
    }

    private void initializeTabs() {
        final IOrderElementModel orderElementModel = getOrderElementModel();

        assignedHoursController.openWindow(orderElementModel);
        manageOrderElementAdvancesController.openWindow(orderElementModel);
        assignedLabelsController.openWindow(orderElementModel);
        assignedCriterionRequirementController.openWindow(orderElementModel);
        assignedMaterialsController.openWindow(orderElementModel
                .getOrderElement());
        assignedTaskQualityFormController.openWindow(orderElementModel);
    }

    private void clearEditWindow() {
{'fraction_non_alphanumeric': 0.07839410856396246, 'fraction_numerical': 0.0008611474046798908, 'mean_word_length': 3.0355901737567406, 'pattern_counts': {'":': 4, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4048936', 'n_tokens_mistral': 8329, 'n_tokens_neox': 8163, 'n_words': 1673}