Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #325 from zanata/zanata-autocomplete
Browse files Browse the repository at this point in the history
Implement custom autocomplete component
  • Loading branch information
carlosmunoz committed Dec 19, 2013
2 parents c65bc4b + 3cdda5a commit e698099
Show file tree
Hide file tree
Showing 22 changed files with 731 additions and 928 deletions.
Expand Up @@ -9,6 +9,8 @@
public interface IndexFieldLabels {
public static final String PROJECT_FIELD = "project";
public static final String ITERATION_FIELD = "iteration";
public static final String ENTITY_STATUS = "status";

/**
* Represents the full path and name of the document
*/
Expand Down
2 changes: 0 additions & 2 deletions zanata-war/src/main/java/org/zanata/action/ProjectAction.java
Expand Up @@ -41,8 +41,6 @@ public class ProjectAction implements Serializable {
private boolean showReadOnly = true;
private boolean showObsolete = false;

private HProject securedEntity = null;

private ProjectPagedListDataModel projectPagedListDataModel =
new ProjectPagedListDataModel(!showActive, !showReadOnly,
!showObsolete);
Expand Down
104 changes: 38 additions & 66 deletions zanata-war/src/main/java/org/zanata/action/ProjectSearch.java
@@ -1,28 +1,25 @@
package org.zanata.action;

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

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.queryParser.ParseException;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.common.EntityStatus;
import org.zanata.dao.ProjectDAO;
import org.zanata.model.HProject;
import org.zanata.security.ZanataIdentity;

import javax.faces.model.DataModel;
import java.io.Serializable;
import java.util.List;

@Name("projectSearch")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
Expand All @@ -32,104 +29,79 @@ public class ProjectSearch implements Serializable {

@Getter
@Setter
private int pageSize = 30;

@Getter
@Setter
private String searchQuery;
private int scrollerPage = 1;

@Getter
@Setter
private List<HProject> searchResults;

@Getter
private int currentPage = 1;
// project slug
private String selectedItem;

@Setter
@Getter
private int resultSize;
private String suggestQuery;

@In
private ProjectDAO projectDAO;

@In
private ZanataIdentity identity;

private QueryProjectPagedListDataModel queryProjectPagedListDataModel =
new QueryProjectPagedListDataModel();

// Count of result to be return as part of autocomplete
private final static int INITIAL_RESULT_COUNT = 5;

public void setCurrentPage(int page) {
if (page < 1)
this.currentPage = 1;
else
this.currentPage = page;
}

/**
* Return results on project search
*
* @param query
*/
public List<SearchResult> suggestProjects(String query) {
searchQuery = query;
public List<SearchResult> suggestProjects() {
List<SearchResult> result = Lists.newArrayList();
if (StringUtils.isEmpty(searchQuery)) {
if (StringUtils.isEmpty(suggestQuery)) {
return result;
}
try {
for (HProject project : projectDAO.searchQuery(searchQuery,
INITIAL_RESULT_COUNT, 0)) {
result.add(new SearchResult(project, searchQuery));
List<HProject> searchResult =
projectDAO
.searchProjects(suggestQuery, INITIAL_RESULT_COUNT,
0,
identity.hasPermission("HProject",
"view-obsolete"));

for (HProject project : searchResult) {
result.add(new SearchResult(project));
}
result.add(new SearchResult(searchQuery));
result.add(new SearchResult());
return result;
} catch (ParseException pe) {
return result;
}
}

@Begin(join = true)
public void search() {
if (StringUtils.isEmpty(searchQuery)) {
return;
}
public int getPageSize() {
return queryProjectPagedListDataModel.getPageSize();
}

try {
searchResults =
projectDAO.searchQuery(searchQuery, pageSize + 1, pageSize
* (currentPage - 1));
} catch (ParseException pe) {
return;
}
// Manually filtering collection as status field is not indexed by
// hibernate search
if (!identity.hasPermission("HProject", "view-obsolete")) {
CollectionUtils.filter(searchResults, new Predicate() {
@Override
public boolean evaluate(Object arg0) {
return ((HProject) arg0).getStatus() != EntityStatus.OBSOLETE;
}
});
}
resultSize = searchResults.size();
public DataModel getProjectPagedListDataModel() {
queryProjectPagedListDataModel.setIncludeObsolete(identity
.hasPermission("HProject", "view-obsolete"));
return queryProjectPagedListDataModel;
}

public String searchAndRedirect() {
search();
return "/search.xhtml";
public void setSearchQuery(String searchQuery) {
queryProjectPagedListDataModel.setQuery(searchQuery);
}

public String getSearchQuery() {
return queryProjectPagedListDataModel.getQuery();
}

@AllArgsConstructor
@NoArgsConstructor
public class SearchResult {
@Getter
private HProject project;

@Getter
private String query;

public SearchResult(String query) {
this.query = query;
}

public boolean isProjectNull() {
return project == null;
}
Expand Down
@@ -0,0 +1,70 @@
/*
*
* * Copyright 2013, Red Hat, Inc. and individual contributors as indicated by the
* * @author tags. See the copyright.txt file in the distribution for a full
* * listing of individual contributors.
* *
* * This 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 software 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.
* *
* * You should have received a copy of the GNU Lesser General Public License
* * along with this software; if not, write to the Free Software Foundation,
* * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* * site: http://www.fsf.org.
*/
package org.zanata.action;

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

import lombok.Getter;
import lombok.Setter;
import org.apache.lucene.queryParser.ParseException;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.zanata.dao.ProjectDAO;
import org.zanata.model.HProject;

public class QueryProjectPagedListDataModel extends
PagedListDataModel<HProject> implements Serializable {
private static final long serialVersionUID = 1L;

@Setter
private boolean includeObsolete;

@Setter
@Getter
private String query;

public QueryProjectPagedListDataModel() {
setPageSize(2);
}

@Override
public DataPage<HProject> fetchPage(int startRow, int pageSize) {
ProjectDAO projectDAO =
(ProjectDAO) Component.getInstance(ProjectDAO.class,
ScopeType.STATELESS);

try {
List<HProject> proj =
projectDAO.searchProjects(query, pageSize, startRow,
includeObsolete);

int projectSize =
projectDAO.getQueryProjectSize(query, includeObsolete);

return new DataPage<HProject>(projectSize, startRow, proj);

} catch (ParseException e) {
return null;
}
}
}

0 comments on commit e698099

Please sign in to comment.