Biblioteca Java - Rev 9
Subversion Repositories:
(root)/Spring/SpringWebMVC/InventoryHibernate/src/main/java/com/linkscreens/inventory/service/InventoryServiceImpl.java
package com.linkscreens.inventory.service;
import com.linkscreens.inventory.dao.InventoryDao;
import com.linkscreens.inventory.entity.FilterParameters;
import com.linkscreens.inventory.entity.InventoryItem;
import com.linkscreens.inventory.pdfgenerator.PDFGenerator;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Service for managing inventory items.
*/
@Service
public class InventoryServiceImpl implements InventoryService {
private InventoryDao inventoryDao;
public void setInventoryDao(InventoryDao inventoryDao) {
this.inventoryDao = inventoryDao;
}
@Transactional
@Override
public void addNewInventoryItem(InventoryItem item) {
inventoryDao.addNewInventoryItem(item);
}
@Transactional
@Override
public InventoryItem getItemById(Long itemId) {
InventoryItem item = inventoryDao.getItemById(itemId);
if (item.getAnLunaAdaugare() != null && !item.getAnLunaAdaugare().isEmpty() && item.getAnLunaAdaugare().contains("/")) {
item.setAnAdaugare(item.getAnLunaAdaugare().split("/")[0]);
item.setLunaAdaugare(item.getAnLunaAdaugare().split("/")[1]);
} else {
item.setAnAdaugare("2010");
item.setLunaAdaugare("01");
}
if (item.getAnLunaPrimaRata() != null && !item.getAnLunaPrimaRata().isEmpty() && item.getAnLunaPrimaRata().contains("/")) {
item.setAnPrimaRata(item.getAnLunaPrimaRata().split("/")[0]);
item.setLunaPrimaRata(item.getAnLunaPrimaRata().split("/")[1]);
} else {
item.setAnPrimaRata("2010");
item.setLunaPrimaRata("01");
}
return item;
}
@Transactional
@Override
public List<InventoryItem> getAllInventoryItems() {
List<InventoryItem> items = inventoryDao.getAllInventoryItems();
return items;
}
@Transactional
@Override
public List<InventoryItem> geFilteredInventoryItems(FilterParameters filter) {
// get all inventory items and calculate totals for each cont and general total
List<InventoryItem> items = inventoryDao.geFilteredInventoryItems(filter);
Map<String, InventoryItem> totalsMap = new HashMap<String, InventoryItem>();
InventoryItem total = new InventoryItem("TOTAL GENERAL", "");
for (InventoryItem item : items) {
if (totalsMap.get(item.getCont()) == null) {
totalsMap.put(item.getCont(), new InventoryItem("TOTAL CONT " + item.getCont(), item.getCont()));
totalsMap.get(item.getCont()).setAnLunaAdaugare(item.getAnLunaAdaugare());
totalsMap.get(item.getCont()).setCont(item.getCont());
}
totalsMap.get(item.getCont()).setValoare(item.getValoare().add(totalsMap.get(item.getCont()).getValoare()));
totalsMap.get(item.getCont()).setAmortizarePrecedenta(item.getAmortizarePrecedenta().add(totalsMap.get(item.getCont()).getAmortizarePrecedenta()));
totalsMap.get(item.getCont()).setAmortizareCurenta(item.getAmortizareCurenta().add(totalsMap.get(item.getCont()).getAmortizareCurenta()));
totalsMap.get(item.getCont()).setAmortizareTotala(item.getAmortizareTotala().add(totalsMap.get(item.getCont()).getAmortizareTotala()));
totalsMap.get(item.getCont()).setValoareRamasa(item.getValoareRamasa().add(totalsMap.get(item.getCont()).getValoareRamasa()));
total.setValoare(total.getValoare().add(item.getValoare()));
total.setAmortizarePrecedenta(total.getAmortizarePrecedenta().add(item.getAmortizarePrecedenta()));
total.setAmortizareCurenta(total.getAmortizareCurenta().add(item.getAmortizareCurenta()));
total.setAmortizareTotala(total.getAmortizareTotala().add(item.getAmortizareTotala()));
total.setValoareRamasa(total.getValoareRamasa().add(item.getValoareRamasa()));
}
for (String key : totalsMap.keySet()) {
items.add(totalsMap.get(key));
}
items.add(total);
Collections.sort(items);
return items;
}
@Transactional
@Override
public String generatePDFFile(FilterParameters filter, File filePath, boolean isFull) {
List<InventoryItem> items = geFilteredInventoryItems(filter);
if (items != null && !items.isEmpty()) {
String fileName = filePath.getAbsolutePath() + "\\"
+ filter.getStartYear() + "_" + filter.getStartMonth() + "_" + String.valueOf(System.currentTimeMillis() + ".pdf");
PDFGenerator.createPDF(fileName, items, isFull);
return fileName;
}
return null;
}
@Transactional
@Override
public void updateInventoryItem(InventoryItem updatedItem) {
if (updatedItem.getAnPrimaRata() != null && updatedItem.getLunaPrimaRata() != null) {
updatedItem.setAnLunaPrimaRata(updatedItem.getAnPrimaRata() + "/" + updatedItem.getLunaPrimaRata());
}
if (updatedItem.getAnAdaugare() != null && updatedItem.getLunaAdaugare() != null) {
updatedItem.setAnLunaAdaugare(updatedItem.getAnAdaugare() + "/" + updatedItem.getLunaAdaugare());
}
inventoryDao.updateInventoryItem(updatedItem);
}
@Transactional
@Override
public void deleteItem(Long itemId) {
inventoryDao.deleteItem(itemId);
}
}
import com.linkscreens.inventory.dao.InventoryDao;
import com.linkscreens.inventory.entity.FilterParameters;
import com.linkscreens.inventory.entity.InventoryItem;
import com.linkscreens.inventory.pdfgenerator.PDFGenerator;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Service for managing inventory items.
*/
@Service
public class InventoryServiceImpl implements InventoryService {
private InventoryDao inventoryDao;
public void setInventoryDao(InventoryDao inventoryDao) {
this.inventoryDao = inventoryDao;
}
@Transactional
@Override
public void addNewInventoryItem(InventoryItem item) {
inventoryDao.addNewInventoryItem(item);
}
@Transactional
@Override
public InventoryItem getItemById(Long itemId) {
InventoryItem item = inventoryDao.getItemById(itemId);
if (item.getAnLunaAdaugare() != null && !item.getAnLunaAdaugare().isEmpty() && item.getAnLunaAdaugare().contains("/")) {
item.setAnAdaugare(item.getAnLunaAdaugare().split("/")[0]);
item.setLunaAdaugare(item.getAnLunaAdaugare().split("/")[1]);
} else {
item.setAnAdaugare("2010");
item.setLunaAdaugare("01");
}
if (item.getAnLunaPrimaRata() != null && !item.getAnLunaPrimaRata().isEmpty() && item.getAnLunaPrimaRata().contains("/")) {
item.setAnPrimaRata(item.getAnLunaPrimaRata().split("/")[0]);
item.setLunaPrimaRata(item.getAnLunaPrimaRata().split("/")[1]);
} else {
item.setAnPrimaRata("2010");
item.setLunaPrimaRata("01");
}
return item;
}
@Transactional
@Override
public List<InventoryItem> getAllInventoryItems() {
List<InventoryItem> items = inventoryDao.getAllInventoryItems();
return items;
}
@Transactional
@Override
public List<InventoryItem> geFilteredInventoryItems(FilterParameters filter) {
// get all inventory items and calculate totals for each cont and general total
List<InventoryItem> items = inventoryDao.geFilteredInventoryItems(filter);
Map<String, InventoryItem> totalsMap = new HashMap<String, InventoryItem>();
InventoryItem total = new InventoryItem("TOTAL GENERAL", "");
for (InventoryItem item : items) {
if (totalsMap.get(item.getCont()) == null) {
totalsMap.put(item.getCont(), new InventoryItem("TOTAL CONT " + item.getCont(), item.getCont()));
totalsMap.get(item.getCont()).setAnLunaAdaugare(item.getAnLunaAdaugare());
totalsMap.get(item.getCont()).setCont(item.getCont());
}
totalsMap.get(item.getCont()).setValoare(item.getValoare().add(totalsMap.get(item.getCont()).getValoare()));
totalsMap.get(item.getCont()).setAmortizarePrecedenta(item.getAmortizarePrecedenta().add(totalsMap.get(item.getCont()).getAmortizarePrecedenta()));
totalsMap.get(item.getCont()).setAmortizareCurenta(item.getAmortizareCurenta().add(totalsMap.get(item.getCont()).getAmortizareCurenta()));
totalsMap.get(item.getCont()).setAmortizareTotala(item.getAmortizareTotala().add(totalsMap.get(item.getCont()).getAmortizareTotala()));
totalsMap.get(item.getCont()).setValoareRamasa(item.getValoareRamasa().add(totalsMap.get(item.getCont()).getValoareRamasa()));
total.setValoare(total.getValoare().add(item.getValoare()));
total.setAmortizarePrecedenta(total.getAmortizarePrecedenta().add(item.getAmortizarePrecedenta()));
total.setAmortizareCurenta(total.getAmortizareCurenta().add(item.getAmortizareCurenta()));
total.setAmortizareTotala(total.getAmortizareTotala().add(item.getAmortizareTotala()));
total.setValoareRamasa(total.getValoareRamasa().add(item.getValoareRamasa()));
}
for (String key : totalsMap.keySet()) {
items.add(totalsMap.get(key));
}
items.add(total);
Collections.sort(items);
return items;
}
@Transactional
@Override
public String generatePDFFile(FilterParameters filter, File filePath, boolean isFull) {
List<InventoryItem> items = geFilteredInventoryItems(filter);
if (items != null && !items.isEmpty()) {
String fileName = filePath.getAbsolutePath() + "\\"
+ filter.getStartYear() + "_" + filter.getStartMonth() + "_" + String.valueOf(System.currentTimeMillis() + ".pdf");
PDFGenerator.createPDF(fileName, items, isFull);
return fileName;
}
return null;
}
@Transactional
@Override
public void updateInventoryItem(InventoryItem updatedItem) {
if (updatedItem.getAnPrimaRata() != null && updatedItem.getLunaPrimaRata() != null) {
updatedItem.setAnLunaPrimaRata(updatedItem.getAnPrimaRata() + "/" + updatedItem.getLunaPrimaRata());
}
if (updatedItem.getAnAdaugare() != null && updatedItem.getLunaAdaugare() != null) {
updatedItem.setAnLunaAdaugare(updatedItem.getAnAdaugare() + "/" + updatedItem.getLunaAdaugare());
}
inventoryDao.updateInventoryItem(updatedItem);
}
@Transactional
@Override
public void deleteItem(Long itemId) {
inventoryDao.deleteItem(itemId);
}
}