001package org.kuali.ole.describe.lookup;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.describe.bo.OleLocationIngestSummaryRecord;
005import org.kuali.rice.krad.lookup.LookupableImpl;
006import org.kuali.rice.krad.util.GlobalVariables;
007import org.kuali.rice.krad.util.KRADConstants;
008import org.kuali.rice.krad.web.form.LookupForm;
009
010import java.sql.Timestamp;
011import java.text.SimpleDateFormat;
012import java.util.ArrayList;
013import java.util.List;
014import java.util.Map;
015
016/**
017 * Created with IntelliJ IDEA.
018 * User: SG7940
019 * Date: 4/18/13
020 * Time: 5:57 PM
021 * To change this template use File | Settings | File Templates.
022 */
023public class OleLocationSummaryLookupableImpl extends LookupableImpl {
024    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleLocationSummaryLookupableImpl.class);
025
026    /**
027     * This method will populate the search criteria and return the search results
028     *
029     * @param form
030     * @param searchCriteria
031     * @param unbounded
032     * @return searchResults(list)
033     */
034    @Override
035    protected List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) {
036        LOG.debug("Inside getSearchResults()");
037        List<?> searchResults;
038        List<OleLocationIngestSummaryRecord> finalSearchResult = new ArrayList<OleLocationIngestSummaryRecord>();
039        List<OleLocationIngestSummaryRecord> oleLocationIngestSummaryRecords = new ArrayList<OleLocationIngestSummaryRecord>();
040        String dateField = searchCriteria.get(OLEConstants.OleLocationIngestSummaryRecord.DATE);
041        if (dateField != null && !dateField.equals("")) {
042            searchCriteria.remove(OLEConstants.OleLocationIngestSummaryRecord.DATE);
043            oleLocationIngestSummaryRecords = (List<OleLocationIngestSummaryRecord>) super.getSearchResults(form, searchCriteria, unbounded);
044            for (OleLocationIngestSummaryRecord oleLocationIngestSummaryRecord : oleLocationIngestSummaryRecords) {
045                if (oleLocationIngestSummaryRecord.getDate() != null) {
046                    Timestamp dateOnly = oleLocationIngestSummaryRecord.getDate();
047                    SimpleDateFormat sdf = new SimpleDateFormat(OLEConstants.OleLocationIngestSummaryRecord.DATE_FORMAT);
048                    String datestr = sdf.format(dateOnly);
049                    if (datestr.equals(dateField)) {
050                        finalSearchResult.add(oleLocationIngestSummaryRecord);
051                    }
052                }
053            }
054            searchCriteria.put(OLEConstants.OleLocationIngestSummaryRecord.DATE, dateField);
055        } else {
056            oleLocationIngestSummaryRecords = (List<OleLocationIngestSummaryRecord>) super.getSearchResults(form, searchCriteria, unbounded);
057            finalSearchResult.addAll(oleLocationIngestSummaryRecords);
058        }
059        searchResults = finalSearchResult;
060        if (searchResults.size() == 0) {
061            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_RECORD_FOUND);
062        }
063        sortSearchResults(form, searchResults);
064        return searchResults;
065    }
066}