public List<SupplyOrderInformation> makeSupplyOrderBasedOnStockpile(int stockpile) {
    List<SupplyOrderInformation> result = new ArrayList<>();

    SupplyOrderHeaderManager supplyOrderHeaderManager = new SupplyOrderHeaderManager();
    SupplyOrderLineManager supplyOrderLineManager = new SupplyOrderLineManager();

    Date currentDate = new Date(System.currentTimeMillis());

    EntityManager entityManager = Utilities.getEntityManagerFactory().createEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<BookPresentation> criteriaQueryBookPresentation =
        criteriaBuilder.createQuery(BookPresentation.class);
    Root<BookPresentation> rootBookPresentation =
        criteriaQueryBookPresentation.from(BookPresentation.class);
    Join<Collection, PublishingHouse> joinCollectionPublishingHouse =
        rootBookPresentation
            .join(BookPresentation_.book)
            .join(Book_.collection)
            .join(Collection_.publishingHouse);
    criteriaQueryBookPresentation.select(rootBookPresentation);
    criteriaQueryBookPresentation.where(
        criteriaBuilder.lt(rootBookPresentation.get(BookPresentation_.stockpile), stockpile));
    List<Order> orderList = new ArrayList<>();
    orderList.add(criteriaBuilder.asc(joinCollectionPublishingHouse.get(PublishingHouse_.id)));
    orderList.add(criteriaBuilder.asc(rootBookPresentation.get(BookPresentation_.id)));
    criteriaQueryBookPresentation.orderBy(orderList);
    TypedQuery<BookPresentation> typedQueryBookPresentation =
        entityManager.createQuery(criteriaQueryBookPresentation);
    List<BookPresentation> bookPresentationList = typedQueryBookPresentation.getResultList();

    PublishingHouse currentPublishingHouse = null;
    SupplyOrderHeader currentSupplyOrderHeader = null;

    for (BookPresentation bookPresentation : bookPresentationList) {
      PublishingHouse publishingHouse =
          bookPresentation.getBook().getCollection().getPublishingHouse();
      if (currentPublishingHouse == null || !currentPublishingHouse.equals(publishingHouse)) {
        SupplyOrderHeader supplyOrderHeader = new SupplyOrderHeader();
        supplyOrderHeader.setIdentificationNumber(Utilities.generateIdentificationNumber(3, 6));
        supplyOrderHeader.setIssueDate(currentDate);
        supplyOrderHeader.setState(Constants.SENT_STATUS);
        supplyOrderHeader.setPublishingHouse(publishingHouse);
        supplyOrderHeaderManager.create(supplyOrderHeader);
        currentPublishingHouse = publishingHouse;
        currentSupplyOrderHeader = supplyOrderHeader;
      }
      SupplyOrderLine supplyOrderLine = new SupplyOrderLine();
      supplyOrderLine.setBookPresentation(bookPresentation);
      supplyOrderLine.setQuantity(stockpile - bookPresentation.getStockpile());
      supplyOrderLine.setSupplyOrderHeader(currentSupplyOrderHeader);
      supplyOrderLineManager.create(supplyOrderLine);
    }
    entityManager.close();

    entityManager = Utilities.getEntityManagerFactory().createEntityManager();
    criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<SupplyOrderLine> criteriaQuerySupplyOrderLine =
        criteriaBuilder.createQuery(SupplyOrderLine.class);
    Root<SupplyOrderLine> rootSupplyOrderLine =
        criteriaQuerySupplyOrderLine.from(SupplyOrderLine.class);
    Join<SupplyOrderLine, SupplyOrderHeader> joinSupplyOrder =
        rootSupplyOrderLine.join(SupplyOrderLine_.supplyOrderHeader);
    criteriaQuerySupplyOrderLine.select(rootSupplyOrderLine);
    criteriaQuerySupplyOrderLine.where(
        criteriaBuilder.equal(
            joinSupplyOrder.get(SupplyOrderHeader_.issueDate), criteriaBuilder.currentDate()));
    criteriaQuerySupplyOrderLine.orderBy(
        criteriaBuilder.asc(joinSupplyOrder.get(SupplyOrderHeader_.id)),
        criteriaBuilder.asc(rootSupplyOrderLine.get(SupplyOrderLine_.id)));
    TypedQuery<SupplyOrderLine> typedQuerySupplyOrderLine =
        entityManager.createQuery(criteriaQuerySupplyOrderLine);
    List<SupplyOrderLine> supplyOrderLines = typedQuerySupplyOrderLine.getResultList();

    for (SupplyOrderLine supplyOrderLine : supplyOrderLines) {
      SupplyOrderInformation supplyOrderInformation = new SupplyOrderInformation();
      supplyOrderInformation.setPublishingHouseId(
          supplyOrderLine.getSupplyOrderHeader().getPublishingHouse().getId());
      supplyOrderInformation.setBookPresentationId(supplyOrderLine.getBookPresentation().getId());
      supplyOrderInformation.setQuantity(supplyOrderLine.getQuantity());
      result.add(supplyOrderInformation);
    }
    entityManager.close();

    return result;
  }
Ejemplo n.º 2
0
  @Override
  @Action(results = {@Result(name = "success", type = "json")})
  public String execute() {
    CriteriaBuilder cb = ipAddressDao.getCriteriaBuilder();
    CriteriaQuery<IpAddress> cq = cb.createQuery(IpAddress.class);
    Root<IpAddress> r = cq.from(IpAddress.class);
    cq.select(r);

    Map<String, Object> params = ActionContext.getContext().getParameters();
    Integer ipAddressRelationId =
        Integer.valueOf(((String[]) params.get("ipAddressRelation_id"))[0]);
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(cb.equal(r.get("ipAddressRelation"), ipAddressRelationId));

    if (searchField != null) {
      if (searchField.equals("id") || searchField.equals("prefixLength")) {
        Integer searchValue = Integer.valueOf(searchString);
        if (searchOper.equals("eq")) {
          predicates.add(cb.equal(r.get(searchField), searchValue));
        } else if (searchOper.equals("ne")) {
          predicates.add(cb.notEqual(r.get(searchField), searchValue));
        } else if (searchOper.equals("lt")) {
          predicates.add(cb.lt(r.get(searchField).as(Integer.class), searchValue));
        } else if (searchOper.equals("gt")) {
          predicates.add(cb.gt(r.get(searchField).as(Integer.class), searchValue));
        }
      } else if (searchField.equals("address")) {
        if (searchOper.equals("eq")) {
          predicates.add(cb.equal(r.get(searchField), searchString));
        } else if (searchOper.equals("ne")) {
          predicates.add(cb.notEqual(r.get(searchField), searchString));
        } else if (searchOper.equals("bw")) {
          predicates.add(cb.like(r.get(searchField).as(String.class), searchString + "%"));
        } else if (searchOper.equals("ew")) {
          predicates.add(cb.like(r.get(searchField).as(String.class), "%" + searchString));
        } else if (searchOper.equals("cn")) {
          predicates.add(cb.like(r.get(searchField).as(String.class), "%" + searchString + "%"));
        }
      }
    }

    cq.where(cb.and(predicates.toArray(new Predicate[0])));

    gridModel = new ArrayList<IpAddressDto>();
    for (IpAddress ipAddr : ipAddressDao.findByCriteria(cq)) {
      gridModel.add(new IpAddressDto(ipAddr));
    }

    records = gridModel.size();

    if (sord != null && sord.length() != 0 && sidx != null && sidx.length() != 0) {
      if (sidx.equalsIgnoreCase("id")) {
        Collections.sort(gridModel, idComparator);
      } else if (sidx.equalsIgnoreCase("address")) {
        Collections.sort(gridModel, addressComparator);
      } else if (sidx.equalsIgnoreCase("prefixLength")) {
        Collections.sort(gridModel, prefixLengthComparator);
      }

      if (sord.equalsIgnoreCase("desc")) {
        Collections.reverse(gridModel);
      }
    }

    if (!loadonce) {
      int to = (rows * page);
      int from = to - rows;
      if (to > records) {
        to = records;
      }
      gridModel = gridModel.subList(from, to);
    }

    // calculate the total pages for the query
    total = (int) Math.ceil((double) records / (double) rows);

    return "success";
  }
  @Override
  @Action(results = {@Result(name = "success", type = "json")})
  public String execute() {
    CriteriaBuilder cb = realNetworkInterfaceDao.getCriteriaBuilder();
    CriteriaQuery<RealNetworkInterface> cq = cb.createQuery(RealNetworkInterface.class);
    Root<RealNetworkInterface> r = cq.from(RealNetworkInterface.class);
    cq.select(r);

    Map<String, Object> params = ActionContext.getContext().getParameters();
    Integer simulationId = Integer.valueOf(((String[]) params.get("simulation_id"))[0]);
    Simulation simConf = simulationDao.findByKey(simulationId);

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(cb.equal(r.join("node").get("network"), simConf.getNetwork().getId()));

    if (searchField != null) {
      if (searchField.equals("id")) {
        Integer searchValue = Integer.valueOf(searchString);
        if (searchOper.equals("eq")) {
          predicates.add(cb.equal(r.get(searchField), searchValue));
        } else if (searchOper.equals("ne")) {
          predicates.add(cb.notEqual(r.get(searchField), searchValue));
        } else if (searchOper.equals("lt")) {
          predicates.add(cb.lt(r.get(searchField).as(Integer.class), searchValue));
        } else if (searchOper.equals("gt")) {
          predicates.add(cb.gt(r.get(searchField).as(Integer.class), searchValue));
        }
      } else if (searchField.equals("name")) {
        if (searchOper.equals("eq")) {
          predicates.add(cb.equal(r.get(searchField), searchString));
        } else if (searchOper.equals("ne")) {
          predicates.add(cb.notEqual(r.get(searchField), searchString));
        } else if (searchOper.equals("bw")) {
          predicates.add(cb.like(r.get(searchField).as(String.class), searchString + "%"));
        } else if (searchOper.equals("ew")) {
          predicates.add(cb.like(r.get(searchField).as(String.class), "%" + searchString));
        } else if (searchOper.equals("cn")) {
          predicates.add(cb.like(r.get(searchField).as(String.class), "%" + searchString + "%"));
        }
      }
    }

    cq.where(cb.and(predicates.toArray(new Predicate[0])));

    gridModel = new ArrayList<RealNetworkInterfaceWithRealNetworkInterfaceConfigurationDto>();
    if (searchField != null && searchField.equals("realNetworkInterfaceConfiguration")) {
      if (searchOper.equals("eq")) {
        for (RealNetworkInterface model : realNetworkInterfaceDao.findByCriteria(cq)) {
          RealNetworkInterfaceConfiguration nifConf =
              simConf.getRealNetworkInterfaceConfigurations().get(model);
          if (nifConf == null) {
            if (searchString.length() != 0) {
              continue;
            }
          } else if (!nifConf.getName().equals(searchString)) {
            continue;
          }
          gridModel.add(
              new RealNetworkInterfaceWithRealNetworkInterfaceConfigurationDto(model, nifConf));
        }
      } else if (searchOper.equals("ne")) {
        for (RealNetworkInterface model : realNetworkInterfaceDao.findByCriteria(cq)) {
          RealNetworkInterfaceConfiguration nifConf =
              simConf.getRealNetworkInterfaceConfigurations().get(model);
          if (nifConf == null) {
            if (searchString.length() != 0) {
              continue;
            }
          } else if (nifConf.getName().equals(searchString)) {
            continue;
          }
          gridModel.add(
              new RealNetworkInterfaceWithRealNetworkInterfaceConfigurationDto(model, nifConf));
        }
      } else if (searchOper.equals("bw")) {
        for (RealNetworkInterface model : realNetworkInterfaceDao.findByCriteria(cq)) {
          RealNetworkInterfaceConfiguration nifConf =
              simConf.getRealNetworkInterfaceConfigurations().get(model);
          if (nifConf == null) {
            if (searchString.length() != 0) {
              continue;
            }
          } else if (!nifConf.getName().startsWith(searchString)) {
            continue;
          }
          gridModel.add(
              new RealNetworkInterfaceWithRealNetworkInterfaceConfigurationDto(model, nifConf));
        }
      } else if (searchOper.equals("ew")) {
        for (RealNetworkInterface model : realNetworkInterfaceDao.findByCriteria(cq)) {
          RealNetworkInterfaceConfiguration nifConf =
              simConf.getRealNetworkInterfaceConfigurations().get(model);
          if (nifConf == null) {
            if (searchString.length() != 0) {
              continue;
            }
          } else if (!nifConf.getName().endsWith(searchString)) {
            continue;
          }
          gridModel.add(
              new RealNetworkInterfaceWithRealNetworkInterfaceConfigurationDto(model, nifConf));
        }
      } else if (searchOper.equals("cn")) {
        for (RealNetworkInterface model : realNetworkInterfaceDao.findByCriteria(cq)) {
          RealNetworkInterfaceConfiguration nifConf =
              simConf.getRealNetworkInterfaceConfigurations().get(model);
          if (nifConf == null) {
            if (searchString.length() != 0) {
              continue;
            }
          } else if (!nifConf.getName().contains(searchString)) {
            continue;
          }
          gridModel.add(
              new RealNetworkInterfaceWithRealNetworkInterfaceConfigurationDto(model, nifConf));
        }
      }
    } else {
      for (RealNetworkInterface model : realNetworkInterfaceDao.findByCriteria(cq)) {
        RealNetworkInterfaceConfiguration nifConf =
            simConf.getRealNetworkInterfaceConfigurations().get(model);
        gridModel.add(
            new RealNetworkInterfaceWithRealNetworkInterfaceConfigurationDto(model, nifConf));
      }
    }

    records = gridModel.size();

    if (sord != null && sord.length() != 0 && sidx != null && sidx.length() != 0) {
      Collections.sort(gridModel, comparators.get(sidx));
      if (sord.equalsIgnoreCase("desc")) {
        Collections.reverse(gridModel);
      }
    }

    if (!loadonce) {
      int to = (rows * page);
      int from = to - rows;
      if (to > records) {
        to = records;
      }
      gridModel = gridModel.subList(from, to);
    }

    // calculate the total pages for the query
    total = (int) Math.ceil((double) records / (double) rows);

    return "success";
  }