org.rro.inventory.service.InventoryServices.java Source code

Java tutorial

Introduction

Here is the source code for org.rro.inventory.service.InventoryServices.java

Source

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 *
 * 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 org.rro.inventory.service;

import java.io.ByteArrayOutputStream;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;
import java.util.logging.Logger;

import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.SingularAttribute;

import org.rro.android.commons.models.ItemsResponse;
import org.rro.android.commons.par.ItemModel;
import org.rro.inventory.commons.models.Item;
import org.rro.inventory.commons.models.Item_;
import org.rro.inventory.commons.models.Label_;
import org.rro.inventory.commons.models.Supplier;
import org.rro.inventory.commons.models.Supplier_;
import org.rro.inventory.data.InventoryRepository;

import com.adp.it.common.exceptions.FatalException;
import com.adp.it.common.exceptions.UserException;
import com.adp.it.common.utils.PDFLabels;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;

@Stateless
public class InventoryServices {

    @Inject
    private Logger log;

    @Inject
    private EntityManager em;

    @Inject
    private InventoryRepository inventoryRepository;

    public void LoadData(String excelFileId) throws Exception {
        log.info("Loading ");
        inventoryRepository.loadDataFromExcel(em, excelFileId);
    }

    public Long getRecordCount(String entityName) throws UserException {
        return inventoryRepository.getRecordCount(em, entityName);
    }

    public ItemsResponse getItems(String[] orderFields, Integer page, Integer pagesize) throws UserException {
        return inventoryRepository.findAllItemsPaged(em, page, pagesize, orderFields);
    }

    public List<Item> findAllItems(String supplierCode) throws UserException {
        return inventoryRepository.findAllItems(em, supplierCode,
                Arrays.asList(new SingularAttribute[] { Item_.description }));
    }

    public List<Supplier> findAllSuppliers() {
        return inventoryRepository.findAllSuppliers(em,
                Arrays.asList(new SingularAttribute[] { Supplier_.description }));
    }

    @Asynchronous
    public Future<Long> addItem(ItemModel item) throws UserException {
        log.info("InventoryServices!!!!!!!!!!!!!!!!!!! addITem - " + item.getCodes().toArray() + " entity manager="
                + em.toString());
        ItemModel retVal = inventoryRepository.addItem(em, item);
        return new AsyncResult<Long>(retVal.getId());
    }

    @Asynchronous
    public Future<Integer> addPicture(String pictureName, byte[] pictureData) throws UserException {
        log.info("InventoryServices!!!!!!!!!!!!!!!!!!! addPicture - " + pictureName);
        int length = inventoryRepository.addPicture(em, pictureName, pictureData);
        return new AsyncResult<Integer>(length);
    }

    public boolean deleteItem(ItemModel item) throws UserException {
        log.info("InventoryServices!!!!!!!!!!!!!!!!!!! deleteITem - " + item.getCodes().toArray()
                + " entity manager=" + em.toString());
        return inventoryRepository.deleteItem(em, item);
    }

    public boolean deletePicture(String pictureName) throws UserException {
        log.info("InventoryServices!!!!!!!!!!!!!!!!!!! deletePicture - " + pictureName);
        return inventoryRepository.deletePicture(pictureName);
    }

    public void printLabel(ByteArrayOutputStream fos) throws UserException {

        long lastLabelId = 0L;
        PDFLabels pl = null;
        try {

            List<Item> items = inventoryRepository.findAllItems(em, "MASFILIO",
                    Arrays.asList(new SingularAttribute[] { Label_.code, Item_.code }));

            if (items != null) {

                for (Item item : items) {
                    if (lastLabelId != item.getType().getLabel().getId().longValue()) {
                        if (pl != null) {
                            //TODO multitracciatooooooooooooooooo
                        }
                        //                  pl = new PDFLabels(item.getType().getLabel().getMmHeigth().floatValue()/10, 
                        //                                   item.getType().getLabel().getMmWidth().floatValue()/10, .1f, .5f, .5f, fos);
                        pl.setAlignment(Element.ALIGN_CENTER, Element.ALIGN_CENTER); //sets the defalut alignment properties for the cells
                        pl.setFont(new Font(FontFamily.COURIER, 8, Font.NORMAL));
                        lastLabelId = item.getType().getLabel().getId().longValue();
                    }

                }

                String text = "Test codiceeee:";
                DecimalFormat df = new DecimalFormat("ABCD5678");

                for (int r = 0; r < 60; r++) {
                    pl.append39(text, df.format(r));
                }

                pl.finnish();

                fos.flush();
                fos.close();

                System.out.println("TERMINATO");

            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new FatalException(e.getMessage(), e);
        }
    }

}