package com.perforce.spark.search;
import java.util.ArrayList;
import java.util.List;
import spark.Request;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.spark.artifact.ArtifactType;
import com.perforce.spark.connection.ConnectionSession;
import com.perforce.spark.depot.DepotFactory;
import com.perforce.spark.depot.DepotModel;
public class SearchQuery {
private String queryRaw;
private List<String> paths = new ArrayList<>();
private List<SearchField> searchFields = new ArrayList<>();
private int startRow = 0;
private int rowCount = 1000;
private String resultFormat = "DETAILED";
private final String userId;
private final String ticket;
public SearchQuery(IOptionsServer p4, Request request)
throws P4JavaException {
this.userId = request.session().attribute(ConnectionSession.USER);
this.ticket = request.session().attribute(ConnectionSession.TICKET);
String qType = SearchID.TYPE.get(request);
if (qType != null && !"_any_".equals(qType)) {
ArtifactType type = ArtifactType.parse(qType);
List<DepotModel> depots = DepotFactory.listByType(p4, type);
for (DepotModel d : depots) {
paths.add(d.getPath());
}
}
String qRepo = SearchID.REPO.get(request);
if (qRepo != null && !"_all_".equals(qRepo)) {
DepotModel depot = DepotFactory.getByName(p4, qRepo);
paths.clear();
paths.add(depot.getPath());
}
String query = "";
String qFile = SearchID.FILE.get(request);
if (qFile != null && !qFile.isEmpty()) {
query += "f:" + qFile + ",";
}
String qContent = SearchID.CONTENT.get(request);
if (qContent != null && !qContent.isEmpty()) {
query += "c:" + qContent + ",";
}
this.queryRaw = query;
}
public String getQueryRaw() {
return queryRaw;
}
public List<String> getPaths() {
return paths;
}
public List<SearchField> getSearchFields() {
return searchFields;
}
public int getStartRow() {
return startRow;
}
public int getRowCount() {
return rowCount;
}
public String getResultFormat() {
return resultFormat;
}
public void setQueryRaw(String queryRaw) {
this.queryRaw = queryRaw;
}
public void setPaths(List<String> paths) {
this.paths = paths;
}
public void setSearchFields(List<SearchField> searchFields) {
this.searchFields = searchFields;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public void setResultFormat(String resultFormat) {
this.resultFormat = resultFormat;
}
public String getUserId() {
return userId;
}
public String getTicket() {
return ticket;
}
}