com.redhat.rhtracking.core.services.DeliveryServiceHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.redhat.rhtracking.core.services.DeliveryServiceHandler.java

Source

/*
 * Copyright (C) 2015 Nuuptech
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.redhat.rhtracking.core.services;

import com.redhat.rhtracking.core.domain.HourType;
import com.redhat.rhtracking.events.EventStatus;
import com.redhat.rhtracking.persistance.services.ConsultantAssignmentPersistanceService;
import com.redhat.rhtracking.persistance.services.DeliveryMatrixPersistanceService;
import com.redhat.rhtracking.persistance.services.DeliveryPersistanceService;
import com.redhat.rhtracking.persistance.services.OpportunityPersistanceService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author marco-g8
 */
public class DeliveryServiceHandler implements DeliveryService {

    private static final Logger logger = Logger.getLogger(DeliveryServiceHandler.class);

    @Autowired
    private ConsultantAssignmentPersistanceService assignmentPersistanceService;
    @Autowired
    private DeliveryPersistanceService deliveryPersistanceService;
    @Autowired
    private OpportunityPersistanceService opportunityPersistanceService;
    @Autowired
    private DeliveryMatrixPersistanceService deliveryMatrixPersistanceService;

    @Override
    public Map<String, Object> assignConsultant(Map<String, Object> event) {
        Map<String, Object> request = new HashMap<>();
        if (event.containsKey("opportunityId")) {
            request.put("id", (long) event.get("opportunityId"));
        } else if (event.containsKey("opportunityIdentifier")) {
            request.put("identifier", (String) event.get("opportunityIdentifier"));
        } else {
            throw new IllegalArgumentException("request must containt opportunityId or opportunityIdentifier");
        }
        Map<String, Object> opportunity = opportunityPersistanceService.findBy(request);
        event.put("deliveryMatrixId", (long) opportunity.get("deliveryMatrixId"));

        // verifica horas correctas

        // obtine horas
        HourType aType = HourType.valueOf((String) event.get("hourType"));
        int aHours = (int) event.get("assignedHours");

        request.clear();
        request.put("id", (long) opportunity.get("id"));
        Map<String, Object> hours = opportunityPersistanceService.listOpportunityHours(request);

        logger.debug("hpurs" + hours);

        for (Map<String, Object> h : (List<Map<String, Object>>) hours.get("list")) {
            HourType t = HourType.valueOf((String) h.get("hourType"));
            if (aType.equals(t)) {
                int aviable = (int) h.get("quantity") - (int) h.get("hoursToAccrued");
                if (aviable < aHours) {
                    Map<String, Object> err = new HashMap<>();
                    err.put("status", EventStatus.EXCEPTION);
                    return err;
                }
                h.put("hoursToAccrued", (int) h.get("hoursToAccrued") + aHours);
                opportunityPersistanceService.updateOpportunityHours(h);
            }
        }

        return assignmentPersistanceService.saveAssignment(event);
    }

    @Override
    public Map<String, Object> listAssignedConsultantsOfOpportunity(Map<String, Object> event) {
        return assignmentPersistanceService.listAllAssignmentsByOpportunity(event);
    }

    @Override
    public Map<String, Object> listAllConsultorAsignations(Map<String, Object> event) {
        return assignmentPersistanceService.listAllAssignmentsByConsultor(event);
    }

    @Override
    public Map<String, Object> listAllDeliveriesOfAssignation(Map<String, Object> event) {
        return deliveryPersistanceService.findAllDeliveriesOfAssignment(event);
    }

    @Override
    public Map<String, Object> registerDeliveredHours(Map<String, Object> event) {
        Map<String, Object> saveDelivery = deliveryPersistanceService.saveDelivery(event);
        if ((EventStatus) saveDelivery.get("status") != EventStatus.SUCCESS) {
            return saveDelivery;
        }

        Map<String, Object> assignment = assignmentPersistanceService
                .findAssignmentById((long) event.get("consultantAssignmentId"));
        int deliveredHours = (int) assignment.get("deliveredHours");
        assignment.put("deliveredHours", deliveredHours + (int) event.get("deliveredHours"));
        Map<String, Object> saveAssignment = assignmentPersistanceService.updateAssignment(assignment);

        // Update opportunity remaining hours
        Map<String, Object> request1 = new HashMap<>();
        request1.put("id", (long) assignment.get("deliveryMatrixId"));
        Map<String, Object> opportunity = opportunityPersistanceService.findByDeliveryMatrixId(request1);
        opportunity.put("accruedHours", (int) opportunity.get("accruedHours") + (int) event.get("deliveredHours"));
        opportunityPersistanceService.updateOpportunity(opportunity);

        // Update opportunity-hours remaining hours
        Map<String, Object> request2 = new HashMap<>();
        request2.put("id", (long) opportunity.get("id"));
        Map<String, Object> listOpportunityHours = opportunityPersistanceService.listOpportunityHours(request2);

        for (Map<String, Object> hour : (List<Map<String, Object>>) listOpportunityHours.get("list")) {
            if (((String) hour.get("hourType")).equals((String) assignment.get("hourType"))) {
                hour.put("accruedHours", (int) hour.get("accruedHours") + (int) event.get("deliveredHours"));
                opportunityPersistanceService.updateOpportunityHours(hour);
                break;
            }
        }

        return saveDelivery;
    }

    @Override
    public Map<String, Object> getAssignation(Map<String, Object> event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map<String, Object> getDelivery(Map<String, Object> event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}