my code stock.com

no

Snippet options

Download: Download snippet as test-snippet.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

/*
 * $Id$
 * Copyright (c) 2004-2016 Optiscan Abakus Oy. All rights reserved.
 *
 * This software is the proprietary information of Optiscan Abakus Oy.
 * Use is subject to license terms.
 */
package com.optiscangroup.warehouse.client.web.helpers;

import fi.abakus.warehouse.model.*;
import fi.abakus.warehouse.services.ProductOrderService;
import fi.abakus.warehouse.services.StorageActionQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.*;

/**
 * This validator checks that the given storage actions can be grouped to form a collection.
 */
public class CreateStorageActionPrePickValidator implements Validator {
    private static final Logger log = LoggerFactory.getLogger(CreateStorageActionPrePickValidator.class);

    protected List<StorageActionCollection> collections;
    protected List<StorageAction> actions;
    private ProductOrderService productOrderService;
    private int totalActionCount;

    private PrintType printType;
    private Storage storage;
    private StorageActionStatus status;
    private String packingMode;
    private String externalStorageName;
    private boolean voiceTask;
    private List<String> statusEqualityList;

    public void setProductOrderService(ProductOrderService productOrderService) {
        this.productOrderService = productOrderService;
    }

    @Override
    public boolean supports(Class clazz) {
        return CreateStorageActionPrePickForm.class.isAssignableFrom(clazz);
    }

    public void setStatusEqualityList(List<String> statusEqualityList) {
        this.statusEqualityList = statusEqualityList;
    }

    protected boolean populateLists(Integer[] actionIds, Errors errors) {
        if (actionIds == null || actionIds.length <= 1) {
            errors.rejectValue("storageActions", "too.few.actions");
            return false;
        }

        Set<Integer> ids = new HashSet<Integer>();
        ids.addAll(Arrays.asList(actionIds));

        collections = new ArrayList<StorageActionCollection>();
        actions = new ArrayList<StorageAction>();

        StorageActionQuery query = new StorageActionQuery();
        query.setPageSize(0);
        query.setIds(ids);

        List<AbstractStorageAction> fetchedActions = productOrderService.getStorageActions(query).getPageElements();

        if (fetchedActions.size() != ids.size()) {
            errors.rejectValue("storageActions", "action.not.found");
            return false;
        }
        for (AbstractStorageAction action : fetchedActions) {
            if (action instanceof StorageActionCollection) {
                collections.add((StorageActionCollection)action);
                totalActionCount += ((StorageActionCollection)action).getActions().size();
            }
            else {
                actions.add((StorageAction)action);
                totalActionCount++;
            }
        }

        return true;
    }

    protected void validateCollections(Errors errors) {
        for (StorageActionCollection collection : collections) {
            validateSingleAction(errors, collection);
        }
    }

    protected void populateValidationData() {
        if (collections.size() >= 1) {
            StorageActionCollection collection = collections.get(0);
            printType = collection.getPrintType();
            storage = collection.getStorage();
            status = collection.getStatus();
            packingMode = collection.getPackingMode();

            if (collection.getActions().isEmpty()) {
                externalStorageName = null;
            }
            else {
                externalStorageName = collection.getActions().iterator().next().getExternalStorageName();
            }
            voiceTask = collection.isVoiceTask();
        }
        else {
            StorageAction action = actions.get(0);
            printType = action.getPrintType();
            storage = action.getStorage();
            status = action.getStatus();
            packingMode = action.getPackingMode();
            externalStorageName = action.getExternalStorageName();
            voiceTask = action.isVoiceTask();
        }
    }

    protected void validatePrintTypeAndMaxNumberOfActions(Errors errors) {
        if (printType != null && printType.getMaxNumberOfAction() != null
                && totalActionCount > printType.getMaxNumberOfAction()) {
            errors.rejectValue("storageActions", "too.much.actions");
        }
    }

    protected boolean checkActionStatuses(StorageActionStatus status, StorageActionStatus status2) {
        if (statusEqualityList != null && !statusEqualityList.isEmpty()) {
            for (String equalStatuses : statusEqualityList) {
                if (StorageActionStatus.containsStatus(status, equalStatuses)
                        && StorageActionStatus.containsStatus(status2, equalStatuses)) {
                    return true;
                }
            }
        }

        return status.equals(status2);
    }

    protected void validateSingleAction(Errors errors, AbstractStorageAction action) {
        if ((printType == null && action.getPrintType() != null)
                || (printType != null && !printType.equals(action.getPrintType()))) {
            errors.rejectValue("storageActions", "different.printTypes");
        }

        // All actions must be Pick type
        if (!StorageActionType.PICK.equals(action.getType())) {
            errors.rejectValue("storageActions", "different.all");
        }

        if (!checkActionStatuses(status, action.getStatus())) {
            errors.rejectValue("storageActions", "different.storageActionStatuses");
        }

        if (packingMode != null && !packingMode.equals(action.getPackingMode())) {
            errors.rejectValue("storageActions", "different.packingModes");
        }
        else if (packingMode == null && action.getPackingMode() != null) {
            errors.rejectValue("storageActions", "different.packingModes");
        }

        if (storage == null) {
            if (action.getStorage() != null) {
                errors.rejectValue("storageActions", "different.storages");
            }
        }
        else if (!storage.equals(action.getStorage())) {
            errors.rejectValue("storageActions", "different.storages");
        }

        if (!safeEquals(externalStorageName, action.getExternalStorageName())) {
            errors.rejectValue("storageActions", "different.externalStorages");
        }

        if (!voiceTask == action.isVoiceTask()) {
            errors.rejectValue("storageActions", "different.voiceTaskType");
        }
    }

    /**
     * Return true if the strings are equal. Treat nulls as empty strings.
     *
     * @param s1 DOCUMENT ME!
     * @param s2 DOCUMENT ME!
     * @return DOCUMENT ME!
     */
    private boolean safeEquals(String s1, String s2) {
        if (s1 == null) {
            s1 = "";
        }
        if (s2 == null) {
            s2 = "";
        }
        return s1.equals(s2);
    }

    protected void validateActions(Errors errors) {
        for (StorageAction action : actions) {
            validateSingleAction(errors, action);
        }
    }

    @Override
    public void validate(Object command, Errors errors) {
        CreateStorageActionPrePickForm form = (CreateStorageActionPrePickForm)command;

        if (log.isDebugEnabled()) {
            log.debug("Validating CreateStorageActionPrePickForm with actions "
                + Arrays.toString(form.getStorageActions()));
        }

        Integer[] actionIds = form.getStorageActions();
        if (!populateLists(actionIds, errors)) {
            if (log.isDebugEnabled()) {
                log.debug("Errors while validating chosen actions to be added to collection! ActionIds: " + actionIds);
            }
            return;
        }

        populateValidationData();

        validateCollections(errors);

        validatePrintTypeAndMaxNumberOfActions(errors);

        validateActions(errors);
    }

    public List<StorageActionCollection> getCollections() {
        return collections;
    }

    public List<StorageAction> getActions() {
        return actions;
    }
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.