public List<ProductionOrderComplete> getProductionOrders( final ProductionOrdersSearchFields fields, final List<Locale> locales ) throws MbiException, NoDataFoundException
{
if ( ProductionOrders.LOGGER.isDebugEnabled() ) {
ProductionOrders.LOGGER.debug( fields + ", " + locales );
}
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<MbiFesta> criteriaQuery = criteriaBuilder.createQuery( MbiFesta.class );
Root<MbiFesta> root = criteriaQuery.from( MbiFesta.class );
List<Predicate> predicates = new ArrayList<>();
if ( fields.getFromProductionAreaId() != null || fields.getToProductionAreaId() != null || fields.getProductionAreaIds() != null ) {
Join<MbiFesta, MbiFeber> mbiFeber = root.join( MbiFesta_.mbiFeber );
if ( fields.getFromProductionAreaId() != null ) {
Predicate condition = criteriaBuilder.ge( mbiFeber.get( MbiFeber_.bereichNr ), fields.getFromProductionAreaId() );
predicates.add( condition );
}
if ( fields.getToProductionAreaId() != null ) {
Predicate condition = criteriaBuilder.le( mbiFeber.get( MbiFeber_.bereichNr ), fields.getToProductionAreaId() );
predicates.add( condition );
}
if ( fields.getProductionAreaIds() != null ) {
Predicate condition = criteriaBuilder.isTrue( mbiFeber.get( MbiFeber_.bereichNr ).in( fields.getProductionAreaIds() ) );
predicates.add( condition );
}
}
if ( fields.getFromSerialNo() != null || fields.getToSerialNo() != null ) {
if ( fields.getFromSerialNo() != null ) {
Predicate condition = criteriaBuilder.ge( root.get( MbiFesta_.prodSerienNr ), fields.getFromSerialNo() );
predicates.add( condition );
}
if ( fields.getToSerialNo() != null ) {
Predicate condition = criteriaBuilder.le( root.get( MbiFesta_.prodSerienNr ), fields.getToSerialNo() );
predicates.add( condition );
}
}
if ( fields.getFromDocumentNumber() != null || fields.getToDocumentNumber() != null ) {
Join<MbiFesta, MbiFebdr> mbiFebdr = root.join( MbiFesta_.mbiFebdr );
if ( fields.getFromDocumentNumber() != null ) {
Predicate condition = criteriaBuilder.ge( mbiFebdr.get( MbiFebdr_.belegNr ), fields.getFromDocumentNumber() );
predicates.add( condition );
}
if ( fields.getToDocumentNumber() != null ) {
Predicate condition = criteriaBuilder.le( mbiFebdr.get( MbiFebdr_.belegNr ), fields.getToDocumentNumber() );
predicates.add( condition );
}
if ( fields.getFromDocumentTypeId() != null || fields.getToDocumentTypeId() != null ) {
Join<MbiFebdr, MbiFebar> mbiFebar = mbiFebdr.join( MbiFebdr_.mbiFebar );
if ( fields.getFromDocumentTypeId() != null ) {
Predicate condition = criteriaBuilder.ge( mbiFebar.get( MbiFebar_.belegArtNr ), fields.getFromDocumentTypeId() );
predicates.add( condition );
}
if ( fields.getToDocumentTypeId() != null ) {
Predicate condition = criteriaBuilder.le( mbiFebar.get( MbiFebar_.belegArtNr ), fields.getToDocumentTypeId() );
predicates.add( condition );
}
}
}
if ( fields.getFinished() != null ) {
if ( fields.getFinished().equals( Boolean.TRUE ) ) {
Predicate condition = criteriaBuilder.equal( root.get( MbiFesta_.status ), ProductionEntityStatus.FINISHED );
predicates.add( condition );
} else {
Predicate condition = criteriaBuilder.between( root.get( MbiFesta_.status ), ProductionEntityStatus.CREATED, ProductionEntityStatus.PARTIALLY_FINISHED );
predicates.add( condition );
}
}
if ( fields.getFromLoadListId() != null || fields.getToLoadListId() != null || fields.getLoadListIds() != null ) {
Join<MbiFesta, MbiFekvw> mbiFekvw = root.join( MbiFesta_.mbiFekvw );
Predicate condition = criteriaBuilder.equal( mbiFekvw.get( MbiFekvw_.verwTabelle ), TableReferenceNo.MBI_AFPOS );
predicates.add( condition );
condition = criteriaBuilder.equal( mbiFekvw.get( MbiFekvw_.aufloesKnz ), ProcurementType.TO_ORDER );
predicates.add( condition );
Subquery<MbiAflap> subquery = criteriaQuery.subquery( MbiAflap.class );
Root<MbiAflap> subRoot = subquery.from( MbiAflap.class );
subquery.select( subRoot );
List<Predicate> subPredicates = new ArrayList<>();
Predicate correlatePredicate = criteriaBuilder.and( criteriaBuilder.equal( subRoot.get( MbiAflap_.afstaSerial ), mbiFekvw.get( MbiFekvw_.stammSerial ) ),
criteriaBuilder.equal( subRoot.get( MbiAflap_.posNr ), mbiFekvw.get( MbiFekvw_.afPosNr ) ) );
subPredicates.add( correlatePredicate );
if ( fields.getFromLoadListId() != null ) {
condition = criteriaBuilder.ge( subRoot.get( MbiAflap_.ladeNr ), fields.getFromLoadListId() );
subPredicates.add( condition );
}
if ( fields.getToLoadListId() != null ) {
condition = criteriaBuilder.le( subRoot.get( MbiAflap_.ladeNr ), fields.getToLoadListId() );
subPredicates.add( condition );
}
if ( fields.getLoadListIds() != null ) {
condition = criteriaBuilder.isTrue( subRoot.get( MbiAflap_.ladeNr ).in( fields.getLoadListIds() ) );
subPredicates.add( condition );
}
subquery.where( subPredicates.toArray( new Predicate[ subPredicates.size() ] ) );
predicates.add( criteriaBuilder.exists( subquery ) );
}
criteriaQuery.distinct( true );
criteriaQuery.where( predicates.toArray( new Predicate[ predicates.size() ] ) );
TypedQuery<MbiFesta> query = this.entityManager.createQuery( criteriaQuery );
List<ProductionOrderComplete> productionOrders = new ArrayList<>();
for ( MbiFesta mbiFesta : query.getResultList() ) {
ProductionOrderComplete productionOrderComplete = this.productionOrder.getComplete( mbiFesta, locales );
productionOrders.add( productionOrderComplete );
}
if ( ProductionOrders.LOGGER.isDebugEnabled() ) {
ProductionOrders.LOGGER.debug( productionOrders );
}
return productionOrders;
}