Java tutorial
/* * 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.core.domain.InvoiceStatus; import com.redhat.rhtracking.events.EventStatus; import com.redhat.rhtracking.persistance.services.InvoicePersistanceService; import com.redhat.rhtracking.persistance.services.OpportunityPersistanceService; import java.math.BigDecimal; import java.util.Date; 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 InvoiceServiceHandler implements InvoiceService { private static final Logger logger = Logger.getLogger(InvoiceServiceHandler.class); @Autowired private OpportunityPersistanceService opportunityPersistanceService; @Autowired private InvoicePersistanceService invoicePersistanceService; @Override public Map<String, Object> saveInvoice(Map<String, Object> event) { logger.debug("ssssssssss " + event); Map<String, Object> response = new HashMap<>(); int totalHours = (int) event.get("platformQuantity") + (int) event.get("middlewareQuantity") + (int) event.get("workshopQuantity"); BigDecimal invoiceTotal = ((BigDecimal) event.get("platformPrice")) .multiply(new BigDecimal((int) event.get("platformQuantity"))); invoiceTotal = invoiceTotal.add(((BigDecimal) event.get("middlewarePrice")) .multiply(new BigDecimal((int) event.get("middlewareQuantity")))); invoiceTotal = invoiceTotal.add(((BigDecimal) event.get("workshopPrice")) .multiply(new BigDecimal((int) event.get("workshopQuantity")))); Map<String, Object> invoice = new HashMap<>(); invoice.put("date", (Date) event.get("date")); invoice.put("deliveryMatrixId", (long) event.get("deliveryMatrixId")); invoice.put("hoursTotal", totalHours); invoice.put("invoiceTotal", invoiceTotal); invoice.put("invoiceRequest", (String) event.get("invoiceRequest")); boolean confirmed = false; if (event.containsKey("invoiceNumber")) { invoice.put("invoiceNumber", (String) event.get("invoiceNumber")); invoice.put("invoiceStatus", InvoiceStatus.CONFIRMED.toString()); confirmed = true; } else { invoice.put("invoiceNumber", null); invoice.put("invoiceStatus", InvoiceStatus.REQUESTED.toString()); } invoice.put("deliveryMatrixId", (long) event.get("deliveryMatrixId")); // get opportunity Map<String, Object> opportunityRequest = new HashMap<>(); opportunityRequest.put("deliveryMatrixId", (long) event.get("deliveryMatrixId")); Map<String, Object> opportunity = opportunityPersistanceService.findBy(opportunityRequest); opportunityRequest.clear(); opportunityRequest.put("id", (long) opportunity.get("id")); Map<String, Object> opportunityHours = opportunityPersistanceService .listOpportunityHours(opportunityRequest); List<Map<String, Object>> hoursList = (List<Map<String, Object>>) opportunityHours.get("list"); // verificar horas for (Map<String, Object> oHours : hoursList) { HourType type = HourType.valueOf((String) oHours.get("hourType")); int aviableHours = (int) oHours.get("quantity") - (int) oHours.get("hoursToBill"); if ((int) event.get(type.toString().toLowerCase() + "Quantity") > aviableHours) { // mas horas de las disponibles response.put("status", EventStatus.EXCEPTION); return response; } } // save invoice Map<String, Object> saveInvoice = invoicePersistanceService.saveInvoice(invoice); if ((EventStatus) saveInvoice.get("status") != EventStatus.SUCCESS) { response.put("status", EventStatus.EXCEPTION); return response; } // update opportunity remaining hours if (confirmed) { opportunity.put("billedHours", (int) opportunity.get("billedHours") + totalHours); } opportunity.put("hoursToBill", (int) opportunity.get("hoursToBill") + totalHours); Map<String, Object> updateOpportunity = opportunityPersistanceService.updateOpportunity(opportunity); if ((EventStatus) updateOpportunity.get("status") != EventStatus.SUCCESS) { throw new RuntimeException("Error at save opportunity"); } // add the hours to invoice hours for (HourType t : HourType.values()) { if ((int) event.get(t.toString().toLowerCase() + "Quantity") < 1) continue; Map<String, Object> hour = new HashMap<>(); hour.put("invoiceOrder", (long) saveInvoice.get("id")); hour.put("quantity", (int) event.get(t.toString().toLowerCase() + "Quantity")); hour.put("hourType", t.toString()); hour.put("unitPrice", (BigDecimal) event.get(t.toString().toLowerCase() + "Price")); hour.put("amount", ((BigDecimal) hour.get("unitPrice")).multiply(new BigDecimal((int) hour.get("quantity")))); Map<String, Object> saveHour = invoicePersistanceService.saveInvoiceHours(hour); if ((EventStatus) saveHour.get("status") != EventStatus.SUCCESS) { response.put("status", EventStatus.EXCEPTION); return response; } // update opportunity hours for (Map<String, Object> oHours : hoursList) { HourType type = HourType.valueOf((String) oHours.get("hourType")); if (type == t) { if (confirmed) { oHours.put("billedHours", (int) oHours.get("billedHours") + (int) hour.get("quantity")); } oHours.put("hoursToBill", (int) oHours.get("hoursToBill") + (int) hour.get("quantity")); Map<String, Object> updateOpportunityHours = opportunityPersistanceService .updateOpportunityHours(oHours); logger.debug("updatr response " + updateOpportunityHours); if ((EventStatus) updateOpportunityHours.get("status") != EventStatus.SUCCESS) { throw new RuntimeException("Error at update opportunity hours"); } break; } } } if (event.containsKey("pdf")) { Map<String, Object> pdf = new HashMap<>(); pdf.put("mimetype", "application/pdf"); pdf.put("data", (byte[]) event.get("pdf")); Map<String, Object> saveAttachment = invoicePersistanceService.saveInvoiceAttachments(pdf); if ((EventStatus) saveAttachment.get("status") != EventStatus.SUCCESS) { response.put("status", EventStatus.EXCEPTION); return response; } } if (event.containsKey("xml")) { Map<String, Object> pdf = new HashMap<>(); pdf.put("mimetype", "application/xml"); pdf.put("data", (byte[]) event.get("xml")); Map<String, Object> saveAttachment = invoicePersistanceService.saveInvoiceAttachments(pdf); if ((EventStatus) saveAttachment.get("status") != EventStatus.SUCCESS) { response.put("status", EventStatus.EXCEPTION); return response; } } response.put("status", com.redhat.rhtracking.events.EventStatus.SUCCESS); return response; } @Override public Map<String, Object> listInvoicesFromDeliveryMatrix(Map<String, Object> event) { return invoicePersistanceService.listInvoicesFromDeliveryMatrix(event); } @Override public Map<String, Object> listAllInvoices(Map<String, Object> event) { return invoicePersistanceService.listAllInvoicesPaged(event); } @Override public Map<String, Object> findInvoice(Map<String, Object> event) { return invoicePersistanceService.findInvoiceById(event); } @Override public Map<String, Object> updateInvoice(Map<String, Object> event) { if (event.containsKey("invoiceNumber")) { event.put("status", InvoiceStatus.CONFIRMED.toString()); Map<String, Object> request = new HashMap<>(); request.put("invoiceOrderId", (long) event.get("id")); Map<String, Object> opportunity = opportunityPersistanceService.findBy(request); Map<String, Object> opportunityHours = opportunityPersistanceService.listOpportunityHours(opportunity); List<Map<String, Object>> listHours = (List<Map<String, Object>>) opportunityHours.get("list"); request.clear(); request.put("id", event.get("id")); Map<String, Object> invoice = invoicePersistanceService.findInvoiceById(event); for (Map<String, Object> ihour : (List<Map<String, Object>>) invoice.get("listHours")) { HourType invoicetype = HourType.valueOf((String) ihour.get("type")); for (Map<String, Object> ohour : listHours) { HourType opportunitytype = HourType.valueOf((String) ohour.get("hourType")); if (invoicetype == opportunitytype) { int billedhours = (int) ohour.get("billedHours") + (int) ihour.get("quantity"); ohour.put("billedHours", billedhours); opportunityPersistanceService.updateOpportunityHours(ohour); break; } } } // update invoice status invoice.put("invoiceStatus", InvoiceStatus.CONFIRMED.toString()); invoice.put("invoiceNumber", event.get("invoiceNumber")); return invoicePersistanceService.updateInvoice(invoice); } throw new UnsupportedOperationException("not supported yet"); } @Override public Map<String, Object> deleteInvoice(Map<String, Object> event) { return invoicePersistanceService.deleteInvoice(event); } }