Package controllers

Source Code of controllers.OrderStateController

package controllers;

import be.objectify.deadbolt.java.actions.Group;
import be.objectify.deadbolt.java.actions.Restrict;
import dao.DataAccessException;
import models.DaoManager;
import models.dto.OrderStateListItemDto;
import models.entities.Order;
import models.entities.OrderState;
import play.Logger;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;
import utils.Utilities;

import java.util.*;

import static play.libs.Json.toJson;

/**
* Created by NotePad.by on 25.08.2014.
*/
@Restrict({@Group("manager"), @Group("supervisor"), @Group("executor"), @Group("delivery-manager")})
public class OrderStateController extends Controller {

    public static Map<String, List<String>> roleAccessibleStates;
    private static Map<String, List<String>> possibleNextStates;

    static {
        possibleNextStates = new HashMap<>();
        possibleNextStates.put("new", new LinkedList<>(Arrays.asList("accepted", "cant-be-completed", "canceled")));
        possibleNextStates.put("accepted", new LinkedList<>(Arrays.asList("processing", "cant-be-completed", "canceled")));
        possibleNextStates.put("processing", new LinkedList<>(Arrays.asList("ready-for-delivery", "cant-be-completed", "canceled")));
        possibleNextStates.put("ready-for-delivery", new LinkedList<>(Arrays.asList("delivery", "cant-be-completed", "canceled")));
        possibleNextStates.put("delivery", new LinkedList<>(Arrays.asList("completed", "cant-be-completed", "canceled")));
        possibleNextStates.put("completed", new ArrayList<String>());
        possibleNextStates.put("canceled", new ArrayList<String>());
        possibleNextStates.put("cant-be-completed", new LinkedList<String>(Arrays.asList("new")));

        roleAccessibleStates = new HashMap<>();
        roleAccessibleStates.put("manager", new LinkedList<>(Arrays.asList("new")));
        roleAccessibleStates.put("executor", new LinkedList<>(Arrays.asList("accepted", "processing")));
        roleAccessibleStates.put("delivery-manager", new LinkedList<>(Arrays.asList("ready-for-delivery", "delivery")));
        roleAccessibleStates.put("supervisor", new LinkedList<>(Arrays.asList(
                "new", "accepted", "processing", "ready-for-delivery", "delivery", "completed", "canceled", "cant-be-completed"
        )));
    }

    @Transactional
    public static Result getAll() {
        try {
            List<OrderState> orderStates = DaoManager.getOrderStateDao().getList();
            List<OrderStateListItemDto> statesDto = OrderStateListItemDto.createFrom(orderStates);
            return ok(toJson(statesDto));
        } catch (DataAccessException e) {
            Logger.error("Failed to get list of order states");
            return notFound("Can't get list order states");
        }
    }

    @Transactional
    public static Result getPossibleStates(Integer orderId) {
        try {
            Order order = DaoManager.getOrderDao().findById(orderId);
            if (order == null) {
                return notFound(Utilities.getJsonErrorNode("can't find order with id " + orderId));
            }
            String currentOrderState = order.getState().getName();
            String currentUserRole = SecurityController.getUser().getRole().getName();
            if (roleAccessibleStates.get(currentUserRole).contains(currentOrderState)) {
                List<String> possibleStates = possibleNextStates.get(currentOrderState);
                return ok(toJson(possibleStates));
            }
            return ok(toJson(new ArrayList()));
        } catch (DataAccessException e) {
            Logger.error("failed to get possible states. can't find order with id " + orderId);
            return notFound("failed to get possible states. can't find order with id " + orderId);
        }
    }

    public static Result getAccessibleStates() {
        String currentUserRole = SecurityController.getUser().getRole().getName();

        return ok(toJson(roleAccessibleStates.get(currentUserRole)));
    }
}
TOP

Related Classes of controllers.OrderStateController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.