net.navasoft.madcoin.backend.services.rest.impl.Provider.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.rest.impl.Provider.java

Source

/*******************************************************************************
 * Copyright 2014 Juan Diego Navarre Gonzalez
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.navasoft.madcoin.backend.services.rest.impl;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import net.navasoft.madcoin.backend.model.entities.impl.ScopeProvider;
import net.navasoft.madcoin.backend.model.entities.impl.WorkRequests;
import net.navasoft.madcoin.backend.services.rest.WorkingLoad;

import org.apache.commons.lang.ArrayUtils;

/**
 * net.navasoft.madcoin.backend.services.rest.impl Class class Provider.
 * Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 8/09/2014 01:45:38 AM
 */
public class Provider implements WorkingLoad {

    /**
     * provider name.
     * 
     * @since 8/09/2014, 01:45:38 AM
     */
    private String providerName;

    /**
     * inicio ventanas.
     * 
     * @since 8/09/2014, 01:45:38 AM
     */
    private Calendar[] inicioVentanas;

    /**
     * fin ventanas.
     * 
     * @since 8/09/2014, 01:45:38 AM
     */
    private Calendar[] finVentanas;

    /**
     * provider preferrence.
     * 
     * @since 8/09/2014, 01:45:38 AM
     */
    private ScopeProvider providerPreferrence;

    /**
     * provider contact.
     * 
     * @since 8/09/2014, 01:45:38 AM
     */
    private String providerContact;

    /**
     * requested order.
     * 
     * @since 8/09/2014, 01:45:38 AM
     */
    private WorkRequests requestedOrder;

    /**
     * {@inheritDoc Object#toString()}.
     * 
     * @return the string
     * @see Object#toString()
     * @since 8/09/2014, 01:45:38 AM
     */
    @Override
    public String toString() {
        return providerName;
    }

    /**
     * Sets the id.
     * 
     * @param id
     *            the new id
     * @since 8/09/2014, 01:45:38 AM
     */
    @Override
    public void setId(String id) {
        providerName = id;
    }

    /**
     * Gets the id.
     * 
     * @return the id
     * @since 8/09/2014, 01:45:38 AM
     */
    @Override
    public String getId() {
        return getProviderName();
    }

    /**
     * Gets the provider name.
     * 
     * @return the provider name
     * @since 8/09/2014, 01:45:38 AM
     */
    public String getProviderName() {
        return providerName;
    }

    /**
     * Sets the policies.
     * 
     * @param policies
     *            the new policies
     * @since 8/09/2014, 01:45:38 AM
     */
    @Override
    public void setPolicies(Object... policies) {
        if (policies.length > 2) {
            inicioVentanas = (Calendar[]) Array.newInstance(Calendar.class, (policies.length - 2) / 2);
            finVentanas = (Calendar[]) Array.newInstance(Calendar.class, (policies.length - 2) / 2);
            Arrays.fill(inicioVentanas, Calendar.getInstance());
            Arrays.fill(finVentanas, Calendar.getInstance());
        }
        providerPreferrence = (ScopeProvider) policies[policies.length - 2];
        providerContact = (String) policies[policies.length - 1];
        policies = ArrayUtils.remove(policies, policies.length - 2);
        policies = ArrayUtils.remove(policies, policies.length - 1);
        int ini = 0;
        int fini = 0;
        for (int contadorMultiplos = 0; contadorMultiplos < policies.length; contadorMultiplos++) {
            if (contadorMultiplos % 2 == 0) {
                java.util.Date horario = (Date) policies[contadorMultiplos];
                inicioVentanas[ini].setTime(horario);
                ini++;
            } else {
                java.util.Date horario = (Date) policies[contadorMultiplos];
                finVentanas[fini].setTime(horario);
                fini++;
            }
        }
    }

    /**
     * Apply policies.
     * 
     * @return true, if successful, otherwise false
     * @since 8/09/2014, 01:45:38 AM
     */
    @Override
    public boolean applyPolicies() {
        if (providerPreferrence.getIdServiceCategory().getIdServiceCategory()
                .equals(requestedOrder.getWorkRequestsPK().getCategoryId())) {
            String param1 = providerPreferrence.getIdBusinessParameter().getParameterType();
            String param2 = providerPreferrence.getIdBusinessParameter2().getParameterType();
            Double minCost = 0d;
            Double maxCost = 0d;
            if (param1.equals("java.lang.Double")) {
                minCost = providerPreferrence.getIdBusinessParameter().getDoubleValue();
            }
            if (param2.equals("java.lang.Double")) {
                maxCost = providerPreferrence.getIdBusinessParameter2().getDoubleValue();
            }
            if (minCost.compareTo(requestedOrder.getMinCost().doubleValue()) <= 0
                    && maxCost.compareTo(requestedOrder.getMaxCost().doubleValue()) <= 0) {
                if (inicioVentanas != null && finVentanas != null) {
                    boolean applicable = (inicioVentanas.length > 0 && finVentanas.length > 0);
                    for (Calendar inicio : inicioVentanas) {
                        applicable &= requestedOrder.getRealArrival().before(inicio.getTime());
                    }
                    for (Calendar fin : finVentanas) {
                        applicable &= requestedOrder.getRealArrival().after(fin.getTime());
                    }
                    applicable &= (providerContact != null);
                    return applicable;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Compare to.
     * 
     * @param o
     *            the o
     * @return the int
     * @since 8/09/2014, 01:45:38 AM
     */
    @Override
    public int compareTo(Object o) {
        return ((Provider) o).getProviderName().compareToIgnoreCase(providerName);
    }

    /**
     * Sets the evaluator.
     * 
     * @param originator
     *            the new evaluator
     * @since 8/09/2014, 01:45:38 AM
     */
    public void setEvaluator(Serializable originator) {
        requestedOrder = (WorkRequests) originator;
    }

    /**
     * Gets the evaluator.
     * 
     * @return the evaluator
     * @since 8/09/2014, 01:45:38 AM
     */
    public WorkRequests getEvaluator() {
        return requestedOrder;
    }

    /**
     * Gets the provider contact.
     * 
     * @return the provider contact
     * @since 8/09/2014, 02:34:33 AM
     */
    public String getProviderContact() {
        return providerContact;
    }
}