<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 */
%>

<%@ page import="com.liferay.portal.kernel.search.Document" %>
<%@ page import="com.liferay.portal.kernel.search.Field" %>
<%@ page import="com.liferay.portal.kernel.search.Hits" %>
<%@ page import="com.liferay.portal.kernel.search.Sort" %>
<%@ page import="com.liferay.portlet.expando.model.ExpandoBridge" %>
<%@ page import="com.liferay.portlet.expando.model.ExpandoColumnConstants" %>
<%@ page import="com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil" %>
<%@ page import="com.liferay.portlet.expando.util.ExpandoBridgeIndexer" %>

<%
ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(company.getCompanyId(), Organization.class.getName());

Enumeration<String> enu = expandoBridge.getAttributeNames();

while (enu.hasMoreElements()) {
	String attributeName = enu.nextElement();

	UnicodeProperties properties = expandoBridge.getAttributeProperties(attributeName);

	if (GetterUtil.getBoolean(properties.getProperty(ExpandoBridgeIndexer.INDEXABLE))) {
		int type = expandoBridge.getAttributeType(attributeName);

		if (type == ExpandoColumnConstants.STRING) {
			if (searchTerms.isAdvancedSearch()) {
				organizationParams.put(attributeName, ParamUtil.getString(request, attributeName));
			}
			else {
				organizationParams.put(attributeName, searchTerms.getKeywords());
			}
		}
	}
}

Sort sort = _getSort(searchContainer.getOrderByCol(), searchContainer.getOrderByType());

Hits hits = null;

if (searchTerms.isAdvancedSearch()) {
	hits = OrganizationLocalServiceUtil.search(company.getCompanyId(), parentOrganizationId, searchTerms.getName(), searchTerms.getType(), searchTerms.getStreet(), searchTerms.getCity(), searchTerms.getZip(), searchTerms.getRegionName(), searchTerms.getCountryName(), organizationParams, searchTerms.isAndOperator(), searchContainer.getStart(), searchContainer.getEnd(), sort);
}
else {
	hits = OrganizationLocalServiceUtil.search(company.getCompanyId(), parentOrganizationId, searchTerms.getKeywords(), organizationParams, searchContainer.getStart(), searchContainer.getEnd(), sort);
}

results = _getResults(hits);
total = hits.getLength();

pageContext.setAttribute("results", results);
pageContext.setAttribute("total", total);
%>

<%!
private static List<Organization> _getResults(Hits hits) throws Exception {
	List<Organization> organizations = new ArrayList<Organization>();

	List<Document> hitsList = hits.toList();

	for (Document doc : hitsList) {
		long organizationId = GetterUtil.getLong(doc.get(Field.ORGANIZATION_ID));

		try {
			organizations.add(OrganizationLocalServiceUtil.getOrganization(organizationId));
		}
		catch (NoSuchOrganizationException nsoe) {
			_log.error("Organization " + organizationId + " does not exist in the search index. Please reindex.");
		}
	}

	return organizations;
}

private static Sort _getSort(String orderByCol, String orderByType) {
	String sortField = "name";

	if (Validator.isNotNull(orderByCol)) {
		if (orderByCol.equals("name")) {
			sortField = "name";
		}
		else if (orderByCol.equals("type")) {
			sortField = "type";
		}
		else {
			sortField = orderByCol;
		}
	}

	if (Validator.isNull(orderByType)) {
		orderByType = "asc";
	}

	return new Sort(sortField, Sort.STRING_TYPE, !orderByType.equalsIgnoreCase("asc"));
}

private static Log _log = LogFactoryUtil.getLog("portal-web.docroot.html.portlet.enterprise_admin.organization_search_results_index.jspf");
%>