office-alexander-logistics/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/CustomSearchController.java

173 lines
6.1 KiB
Java

/*******************************************************************************
* Imixs Workflow Technology
* Copyright (C) 2003, 2008 Imixs Software Solutions GmbH,
* http://www.imixs.com
*
* 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 can receive a copy of the GNU General Public
* License at http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Imixs Software Solutions GmbH - initial API and implementation
* Ralph Soika
*
*******************************************************************************/
package com.alexanderlogistics;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Named;
import org.imixs.workflow.office.views.SearchEvent;
/**
* Der CustomSearchController reagiert auf SeachEvents und erweitert das Search
* Query um Rechnungbetrag von-bis.
*
* @author rsoika
* @version 1.0
*/
@Named
@ConversationScoped
public class CustomSearchController implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Erweitere Query
*
* _amount_brutto_from, _amount_brutto_to
*
* @param searchEvent
*
**/
/*
* Aktuell ist es nicht möglich nach Long oder Double zu suchen. Hierzu muss die
* LuceneUpdate Funktion in Imixs-Workflow verändert werden
*/
public void onSearchEvent(@Observes SearchEvent searchEvent) {
String query = searchEvent.getQuery();
float invoiceTotal = searchEvent.getSearchFilter().getItemValueFloat("invoice.total");
Date invoiceDate = searchEvent.getSearchFilter().getItemValueDate("invoice.date");
Date dueDate = searchEvent.getSearchFilter().getItemValueDate("invoice.duedate");
Date closingDate = searchEvent.getSearchFilter().getItemValueDate("invoice.closingdate");
String cdtrNumber = searchEvent.getSearchFilter().getItemValueString("cdtr.number");
// invoice.total
if (invoiceTotal != 0) {
// format form and to with at least one digit
DecimalFormat decimalFormat = new DecimalFormat("#.00");
query += " (invoice.total:" + decimalFormat.format(invoiceTotal) + ")";
}
// invoice.date
if (invoiceDate != null) {
query += " (invoice.date:" + CustomSearchController.getRangeDay(invoiceDate) + ")";
}
// invoice.duedate
if (dueDate != null) {
query += " (invoice.duedate:" + CustomSearchController.getRangeDay(dueDate) + ")";
}
// invoice.closingdate
if (closingDate != null) {
query += " ($lasteventdate:" + CustomSearchController.getRangeMonth(closingDate) + ")";
}
// dbtr.number
if (cdtrNumber != null && !cdtrNumber.isEmpty()) {
cdtrNumber = cdtrNumber.trim();
if (!cdtrNumber.startsWith("K")) {
cdtrNumber = "K" + cdtrNumber;
}
query += " (cdtr.number:" + cdtrNumber + ")";
}
// Udpate query
searchEvent.setQuery(query);
}
private static String getRangeDay(Date dueDate) {
// Setze die Uhrzeit auf Mitternacht (00:00)
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(dueDate);
startCalendar.set(Calendar.HOUR_OF_DAY, 0);
startCalendar.set(Calendar.MINUTE, 0);
startCalendar.set(Calendar.SECOND, 0);
startCalendar.set(Calendar.MILLISECOND, 0);
Date startDate = startCalendar.getTime();
// Setze die Uhrzeit auf 23:59:59.999
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(dueDate);
endCalendar.set(Calendar.HOUR_OF_DAY, 23);
endCalendar.set(Calendar.MINUTE, 59);
endCalendar.set(Calendar.SECOND, 59);
endCalendar.set(Calendar.MILLISECOND, 999);
Date endDate = endCalendar.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmm");
String dateRange = "[" + dateformat.format(startDate) + " TO "
+ dateformat.format(endDate) + "]";
return dateRange;
}
private static String getRangeMonth(Date dueDate) {
// Setze die Uhrzeit auf Mitternacht (00:00)
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(dueDate);
startCalendar.set(Calendar.HOUR_OF_DAY, 0);
startCalendar.set(Calendar.MINUTE, 0);
startCalendar.set(Calendar.SECOND, 0);
startCalendar.set(Calendar.MILLISECOND, 0);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
Date startDate = startCalendar.getTime();
// Setze die Uhrzeit auf 23:59:59.999
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(dueDate);
endCalendar.set(Calendar.HOUR_OF_DAY, 23);
endCalendar.set(Calendar.MINUTE, 59);
endCalendar.set(Calendar.SECOND, 59);
endCalendar.set(Calendar.MILLISECOND, 999);
endCalendar.set(Calendar.DAY_OF_MONTH, 1);
endCalendar.add(Calendar.MONTH, 1);
endCalendar.add(Calendar.DAY_OF_MONTH, -1);
Date endDate = endCalendar.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmm");
String dateRange = "[" + dateformat.format(startDate) + " TO "
+ dateformat.format(endDate) + "]";
return dateRange;
}
}