package org.mifos.application.customer.center.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mifos.application.NamedQueryConstants;
import org.mifos.application.customer.center.business.CenterBO;
import org.mifos.application.customer.util.helpers.CustomerConstants;
import org.mifos.application.customer.util.helpers.CustomerLevel;
import org.mifos.application.customer.util.helpers.CustomerStatus;
import org.mifos.application.customer.util.helpers.Param;
import org.mifos.application.personnel.business.PersonnelBO;
import org.mifos.application.personnel.persistence.PersonnelPersistence;
import org.mifos.application.personnel.util.helpers.PersonnelConstants;
import org.mifos.framework.exceptions.HibernateSearchException;
import org.mifos.framework.exceptions.PersistenceException;
import org.mifos.framework.hibernate.helper.QueryFactory;
import org.mifos.framework.hibernate.helper.QueryInputs;
import org.mifos.framework.hibernate.helper.QueryResult;
import org.mifos.framework.persistence.Persistence;

public class CenterPersistence extends Persistence {

	public boolean isCenterExists(String name) throws PersistenceException {
		Map<String, Object> queryParameters = new HashMap<String, Object>();
		queryParameters.put(CustomerConstants.DISPLAY_NAME, name);
		List queryResult = executeNamedQuery(
				NamedQueryConstants.GET_CENTER_COUNT_BY_NAME, queryParameters);
		return ((Number) queryResult.get(0)).longValue() > 0;
	}

	public CenterBO getCenter(Integer customerId) throws PersistenceException {
		return (CenterBO) getPersistentObject(CenterBO.class, customerId);
	}

	public CenterBO findBySystemId(String globalCustNum)
			throws PersistenceException {
		Map<String, String> queryParameters = new HashMap<String, String>();
		CenterBO center = null;
		queryParameters.put("globalCustNum", globalCustNum);
		List<CenterBO> queryResult = executeNamedQuery(
				NamedQueryConstants.GET_CENTER_BY_SYSTEMID, queryParameters);
		if (null != queryResult && queryResult.size() > 0) {
			center = queryResult.get(0);
		}
		return center;
	}

	public QueryResult search(String searchString,Short userId) throws PersistenceException {
		String[] namedQuery = new String[2];
		List<Param> paramList = new ArrayList<Param>();
		QueryInputs queryInputs = new QueryInputs();
		QueryResult queryResult = QueryFactory
				.getQueryResult(PersonnelConstants.USER_LIST);
		PersonnelBO user = new PersonnelPersistence().getPersonnel(userId);
		String officeSearchId = user.getOffice().getSearchId();
		namedQuery[0]=NamedQueryConstants.CENTER_SEARCH_COUNT;
		namedQuery[1]=NamedQueryConstants.CENTER_SEARCH;			
		paramList.add(typeNameValue("String","SEARCH_ID",officeSearchId+"%"));	
		paramList.add(typeNameValue("String","CENTER_NAME",searchString+"%"));
		paramList.add(typeNameValue("Short","LEVEL_ID",CustomerLevel.CENTER.getValue()));
		paramList.add(
			typeNameValue("Short","STATUS_ID",
				CustomerStatus.CENTER_ACTIVE.getValue()));
		paramList.add(typeNameValue("Short","USER_ID",userId));
		paramList.add(typeNameValue("Short","USER_LEVEL_ID",user.getLevelEnum()
				.getValue()));
		paramList.add(typeNameValue("Short","LO_LEVEL_ID",PersonnelConstants.LOAN_OFFICER));
		String[] aliasNames = {"parentOfficeId" , "parentOfficeName" , "centerSystemId" , "centerName"};
		queryInputs.setQueryStrings(namedQuery);
		queryInputs.setPath("org.mifos.application.customer.center.util.helpers.CenterSearchResults");
		queryInputs.setAliasNames(aliasNames);	
		queryInputs.setParamList(paramList);
		try {
			queryResult.setQueryInputs(queryInputs);
		} catch (HibernateSearchException e) {
			throw new PersistenceException(e);
		}
		return queryResult;

	}
}

@@ -1030,16 +1030,75 @@
 Result;%0A
+import org.mifos.framework.hibernate.helper.SessionHolder;%0A
 import o
@@ -1124,24 +1124,31 @@
 persistence.
+Session
 Persistence;
@@ -1188,16 +1188,23 @@
 extends 
+Session
 Persiste
@@ -1210,16 +1210,146 @@
 ence %7B%0A%0A
+%09public CenterPersistence(SessionHolder sessionHolder) %7B%0A%09%09super(sessionHolder);%0A%09%7D%0A%0A%09public CenterPersistence() %7B%0A%09%09super();%0A%09%7D%0A%0A
 %09public 

Make CenterPersistence inherit from SessionPersistence not Persistence.