/*
 * This file is part of NavalPlan
 *
 * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolóxico de Galicia
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.navalplanner.web.common.components.finders;

import static org.navalplanner.web.I18nHelper._;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.navalplanner.business.common.IOnTransaction;
import org.navalplanner.business.resources.daos.ICriterionDAO;
import org.navalplanner.business.resources.entities.Criterion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class CriterionMultipleFiltersFinder extends MultipleFiltersFinder {

    @Autowired
    private ICriterionDAO criterionDAO;

    private static final List<Criterion> criterionList = new ArrayList<Criterion>();

    private IFilterEnum criterionFilterEnum = new IFilterEnum() {
        @Override
        public String toString() {
            return _("criterion");
        }
    };

    @Transactional(readOnly = true)
    public void init() {
        getAdHocTransactionService()
                .runOnReadOnlyTransaction(new IOnTransaction<Void>() {
            @Override
                    public Void execute() {
                        loadCriteria();
                        return null;
                    }
                });
    }

    @Transactional(readOnly = true)
    private void loadCriteria() {
        criterionList.clear();
        criterionList.addAll(criterionDAO.getAll());
    }

    @Override
    public List<FilterPair> getFirstTenFilters() {
        Iterator<Criterion> iteratorCriterion = criterionList.iterator();
        while(iteratorCriterion.hasNext() && getListMatching().size() < 10) {
            Criterion criterion = iteratorCriterion.next();
            getListMatching().add(new FilterPair(
                    criterionFilterEnum, criterion.getName(), criterion));
        }
        addNoneFilter();
        return getListMatching();
    }

    @Override
    public List<FilterPair> getMatching(String filter) {
        getListMatching().clear();
        if ((filter != null) && (!filter.isEmpty())) {
            filter = StringUtils.deleteWhitespace(filter.toLowerCase());
            searchInCriteria(filter);
        }
        addNoneFilter();
        return getListMatching();

    }
    private void searchInCriteria(String filter) {
        for(Criterion criterion : criterionList) {
            String name = StringUtils.deleteWhitespace(
                    criterion.getName().toLowerCase());
            if(name.contains(filter)) {
                getListMatching().add(new FilterPair(
                        criterionFilterEnum, criterion.getName(), criterion));
            }
        }
    }

}

@@ -962,36 +962,8 @@
 _;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -1007,16 +1007,54 @@
 il.List;
+%0Aimport java.util.concurrent.Callable;
 %0A%0Aimport
@@ -1128,28 +1128,116 @@
 ess.
-common.IOnTransactio
+hibernate.notification.IAutoUpdatedSnapshot;%0Aimport org.navalplanner.business.hibernate.notification.ReloadO
 n;%0Ai
@@ -1639,21 +1639,29 @@
 ate 
-static final 
+IAutoUpdatedSnapshot%3C
 List
@@ -1671,16 +1671,17 @@
 iterion%3E
+%3E
  criteri
@@ -1690,37 +1690,8 @@
 List
- = new ArrayList%3CCriterion%3E()
 ;%0A%0A 
@@ -1934,258 +1934,298 @@
     
-getAdHocTransactionService()%0A                .runOnReadOnlyTransaction(new IOnTransaction%3CVoid%3E() %7B%0A            @Override%0A                    public Void execute() %7B
+criterionList = getSnapshotRefresher().takeSnapshot(%0A                onTransaction(getCriterionListCallable()),%0A                ReloadOn.onChangeOf(Criterion.class));
 %0A    
+%7D%0A%0A
     
-                loadCriteria();%0A                        return null;%0A       
+private Callable%3CList%3CCriterion%3E%3E getCriterionListCallable() %7B%0A        return new Callable%3CList%3CCriterion%3E%3E() %7B%0A
     
@@ -2224,34 +2224,41 @@
 ) %7B%0A            
- %7D
+@Override
 %0A               
@@ -2258,175 +2258,126 @@
     
-    %7D);%0A    %7D%0A%0A    @Transactional(readOnly = true)%0A    private void loadCriteria() %7B%0A        criterionList.clear();%0A        criterionList.addAll(criterionDAO.getAll())
+public List%3CCriterion%3E call() throws Exception %7B%0A                return criterionDAO.getAll();%0A            %7D%0A        %7D
 ;%0A  
@@ -2507,16 +2507,44 @@
 rionList
+.getValue()%0A                
 .iterato
@@ -3297,16 +3297,17 @@
      for
+ 
 (Criteri
@@ -3334,16 +3334,27 @@
 rionList
+.getValue()
 ) %7B%0A    

Use mechanism for auto updating snapshots of data in CriterionMultipleFiltersFinder