/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.action;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.gluu.jsf2.message.FacesMessages;
import org.gluu.jsf2.service.ConversationService;
import org.gluu.oxtrust.ldap.service.IPersonService;
import org.gluu.oxtrust.model.GluuCustomPerson;
import org.gluu.oxtrust.util.OxTrustConstants;
import org.slf4j.Logger;
import org.xdi.service.security.Secure;
import org.xdi.util.Util;

/**
 * Action class for search persons
 * 
 * @author Yuriy Movchan Date: 10.22.2010
 */
@Named
@ConversationScoped
@Secure("#{permissionService.hasPermission('person', 'access')}")
public class SearchPersonAction implements Serializable {

	private static final long serialVersionUID = -4672682869487324438L;

	@Inject
	private Logger log;

	@Inject
	private FacesMessages facesMessages;

	@Inject
	private ConversationService conversationService;

	@NotNull
	@Size(min = 2, max = 30, message = "Length of search string should be between 2 and 30")
	private String searchPattern;

	private String oldSearchPattern;

	private List<GluuCustomPerson> personList;

	@Inject
	private IPersonService personService;

	public String start() {
		return search();
	}

	public String search() {
		if (Util.equals(this.oldSearchPattern, this.searchPattern)) {
			return OxTrustConstants.RESULT_SUCCESS;
		}

		try {
			this.personList = personService.searchPersons(this.searchPattern);
			this.oldSearchPattern = this.searchPattern;
			this.searchPattern="";
		} catch (Exception ex) {
			log.error("Failed to find persons", ex);
			
			facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to find persons");
			conversationService.endConversation();

			return OxTrustConstants.RESULT_FAILURE;
		}

		return OxTrustConstants.RESULT_SUCCESS;
	}

	public String getSearchPattern() {
		return searchPattern;
	}

	public void setSearchPattern(String searchPattern) {
		this.searchPattern = searchPattern;
	}

	public List<GluuCustomPerson> getPersonList() {
		return personList;
	}

}

@@ -382,95 +382,8 @@
 med;
-%0Aimport javax.validation.constraints.NotNull;%0Aimport javax.validation.constraints.Size;
 %0A%0Aim
@@ -1168,108 +1168,8 @@
 e;%0A%0A
-%09@NotNull%0A%09@Size(min = 2, max = 30, message = %22Length of search string should be between 2 and 30%22)%0A
 %09pri
@@ -1223,32 +1223,71 @@
 SearchPattern;%0A%0A
+%09private boolean firstLaunch = false;%0A%0A
 %09private List%3CGl
@@ -1385,24 +1385,46 @@
 g start() %7B%0A
+%09%09firstLaunch = true;%0A
 %09%09return sea
@@ -1460,16 +1460,283 @@
 rch() %7B%0A
+%09%09if (!firstLaunch && (this.searchPattern.isEmpty() %7C%7C this.searchPattern.length() %3C 2)) %7B%0A%09%09%09firstLaunch = false;%0A%09%09%09facesMessages.add(FacesMessage.SEVERITY_ERROR, %22Length of search string should be between 2 and 30%22);%0A%09%09%09return OxTrustConstants.RESULT_SUCCESS;%0A%09%09%7D%0A
 %09%09if (Ut
@@ -1791,16 +1791,40 @@
 ern)) %7B%0A
+%09%09%09firstLaunch = false;%0A
 %09%09%09retur
@@ -1862,17 +1862,16 @@
 SS;%0A%09%09%7D%0A
-%0A
 %09%09try %7B%0A
@@ -2012,13 +2012,39 @@
 tern
-=
+ = 
 %22%22;%0A
+%09%09%09firstLaunch = false;%0A
 %09%09%7D 
@@ -2114,12 +2114,9 @@
 x);%0A
-%09%09%09
 %0A
+
 %09%09%09f
@@ -2230,16 +2230,39 @@
 tion();%0A
+%09%09%09firstLaunch = false;
 %0A%09%09%09retu

Fix search typo #1131