Skip to content
This repository has been archived by the owner on Jul 2, 2018. It is now read-only.

Commit

Permalink
https://github.com/MPDL-Innovations/spot/issues/490
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiensaquet committed Apr 26, 2016
1 parent 5a2a4c0 commit 4d1b7d0
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 259 deletions.
116 changes: 61 additions & 55 deletions src/main/java/de/mpg/imeji/logic/search/SearchQueryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ public static SearchQuery parseStringQuery(String query) throws UnprocessableErr
* @param query
* @return
* @throws IOException
* @throws UnprocessableError
*/
public static SearchQuery parseStringQueryDecoded(String query) throws IOException {
public static SearchQuery parseStringQueryDecoded(String query) throws UnprocessableError {
SearchQuery searchQuery = new SearchQuery();
String subQuery = "";
String scString = "";
Expand All @@ -121,62 +122,67 @@ public static SearchQuery parseStringQueryDecoded(String query) throws IOExcepti
StringParser simpleMdParser = new StringParser(SEARCH_METADATA_SIMPLE_PATTERN);
StringParser mdParser = new StringParser(SEARCH_METADATA_PATTERN);
StringParser pairParser = new StringParser(SEARCH_PAIR_PATTERN);
while ((c = reader.read()) != -1) {
if (bracketsOpened - bracketsClosed != 0) {
subQuery += (char) c;
} else {
scString += (char) c;
}
if (c == '(') {
hasBracket = true;
bracketsOpened++;
}
if (c == ')') {
bracketsClosed++;
scString = "";
}
if (scString.trim().equals("AND") || scString.trim().equals("OR")) {
searchQuery.getElements()
.add(new SearchLogicalRelation(LOGICAL_RELATIONS.valueOf(scString.trim())));
scString = "";
}
if (scString.trim().equals("NOT")) {
not = true;
scString = "";
}
if (hasBracket && (bracketsOpened - bracketsClosed == 0)) {
SearchQuery subSearchQuery = parseStringQueryDecoded(subQuery);
if (!subSearchQuery.isEmpty()) {
SearchGroup searchGroup = new SearchGroup();
searchGroup.getGroup().addAll(parseStringQueryDecoded(subQuery).getElements());
searchQuery.getElements().add(searchGroup);
subQuery = "";
try {
while ((c = reader.read()) != -1) {
if (bracketsOpened - bracketsClosed != 0) {
subQuery += (char) c;
} else {
scString += (char) c;
}
if (c == '(') {
hasBracket = true;
bracketsOpened++;
}
if (c == ')') {
bracketsClosed++;
scString = "";
}
if (scString.toUpperCase().trim().equals("AND")
|| scString.toUpperCase().trim().equals("OR")) {
searchQuery.getElements().add(
new SearchLogicalRelation(LOGICAL_RELATIONS.valueOf(scString.toUpperCase().trim())));
scString = "";
}
if (scString.toUpperCase().trim().equals("NOT")) {
not = true;
scString = "";
}
if (hasBracket && (bracketsOpened - bracketsClosed == 0)) {
SearchQuery subSearchQuery = parseStringQueryDecoded(subQuery);
if (!subSearchQuery.isEmpty()) {
SearchGroup searchGroup = new SearchGroup();
searchGroup.getGroup().addAll(parseStringQueryDecoded(subQuery).getElements());
searchQuery.getElements().add(searchGroup);
subQuery = "";
}
}
if (simpleMdParser.find(scString)) {
searchQuery.addPair(new SearchSimpleMetadata(simpleMdParser.getGroup(1),
stringOperator2SearchOperator(simpleMdParser.getGroup(2)), simpleMdParser.getGroup(3),
not));
not = false;
scString = "";
} else if (mdParser.find(scString)) {
SearchOperators operator = stringOperator2SearchOperator(mdParser.getGroup(3));
String value = mdParser.getGroup(4);
value = value.startsWith("\"") ? value + "\"" : value;
SearchFields field = SearchFields.valueOf(mdParser.getGroup(2));
URI statementId = ObjectHelper.getURI(Statement.class, mdParser.getGroup(1));
searchQuery.addPair(new SearchMetadata(field, operator, value, statementId, not));
not = false;
scString = "";
} else if (pairParser.find(scString)) {
SearchOperators operator = stringOperator2SearchOperator(pairParser.getGroup(2));
SearchFields field = SearchFields.valueOf(pairParser.getGroup(1));
String value = pairParser.getGroup(3);
value = value.startsWith("\"") ? value + "\"" : value;
searchQuery.addPair(new SearchPair(field, operator, value, not));
scString = "";
not = false;
}
}
if (simpleMdParser.find(scString)) {
searchQuery.addPair(new SearchSimpleMetadata(simpleMdParser.getGroup(1),
stringOperator2SearchOperator(simpleMdParser.getGroup(2)), simpleMdParser.getGroup(3),
not));
not = false;
scString = "";
} else if (mdParser.find(scString)) {
SearchOperators operator = stringOperator2SearchOperator(mdParser.getGroup(3));
String value = mdParser.getGroup(4);
value = value.startsWith("\"") ? value + "\"" : value;
SearchFields field = SearchFields.valueOf(mdParser.getGroup(2));
URI statementId = ObjectHelper.getURI(Statement.class, mdParser.getGroup(1));
searchQuery.addPair(new SearchMetadata(field, operator, value, statementId, not));
not = false;
scString = "";
} else if (pairParser.find(scString)) {
SearchOperators operator = stringOperator2SearchOperator(pairParser.getGroup(2));
SearchFields field = SearchFields.valueOf(pairParser.getGroup(1));
String value = pairParser.getGroup(3);
value = value.startsWith("\"") ? value + "\"" : value;
searchQuery.addPair(new SearchPair(field, operator, value, not));
scString = "";
not = false;
}
} catch (IOException e) {
throw new UnprocessableError(e);
}
if (!"".equals(query) && searchQuery.isEmpty()) {
searchQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ private static Object toESEntity(Object obj, String dataType) {
private static Item setAlbums(Item item) {
AlbumController c = new AlbumController();
SearchQuery q = new SearchQuery();
q.addPair(new SearchPair(SearchFields.member, SearchOperators.EQUALS, item.getId().toString(),
false));
try {
q.addPair(new SearchPair(SearchFields.member, SearchOperators.EQUALS, item.getId().toString(),
false));
} catch (UnprocessableError e) {
LOGGER.error("Error searching for albums of item " + item.getIdString(), e);
}
item.setAlbums(c.search(q, Imeji.adminUser, null, -1, 0, null).getResults());
return item;
}
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/de/mpg/imeji/logic/search/model/SearchElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import de.mpg.imeji.exceptions.UnprocessableError;
import de.mpg.imeji.logic.search.model.SearchLogicalRelation.LOGICAL_RELATIONS;

/**
Expand All @@ -24,15 +25,16 @@ public enum SEARCH_ELEMENTS {
* Add a {@link LOGICAL_RELATIONS} after a {@link SearchElement}
*
* @param lr
* @throws UnprocessableError
*/
public void addLogicalRelation(LOGICAL_RELATIONS lr) {
public void addLogicalRelation(LOGICAL_RELATIONS lr) throws UnprocessableError {
if (!hasElements()) {
throw new RuntimeException("Operation not allowed for " + getType());
throw new UnprocessableError("Operation not allowed for " + getType());
}
if (!isEmpty() && !SEARCH_ELEMENTS.LOGICAL_RELATIONS.equals(getTypeOfLastElement())) {
getElements().add(new SearchLogicalRelation(lr));
} else if (SEARCH_ELEMENTS.LOGICAL_RELATIONS.equals(getTypeOfLastElement())) {
throw new RuntimeException(
throw new UnprocessableError(
"Wrong search query: Logical relations can not be added after a logical relation");
}
}
Expand All @@ -41,15 +43,16 @@ public void addLogicalRelation(LOGICAL_RELATIONS lr) {
* Add a {@link SearchPair} after a {@link SearchElement}
*
* @param pair
* @throws UnprocessableError
*/
public void addPair(SearchPair pair) {
public void addPair(SearchPair pair) throws UnprocessableError {
if (!hasElements()) {
throw new RuntimeException("Operation not allowed for " + getType());
throw new UnprocessableError("Operation not allowed for " + getType());
}
if (isEmpty() || SEARCH_ELEMENTS.LOGICAL_RELATIONS.equals(getTypeOfLastElement())) {
getElements().add(pair);
} else {
throw new RuntimeException(
throw new UnprocessableError(
"Wrong search query. A pair should be added after a logical relation or a the begining of the query!");
}
}
Expand All @@ -58,15 +61,16 @@ public void addPair(SearchPair pair) {
* Add a {@link SearchGroup} after a {@link SearchElement}
*
* @param group
* @throws UnprocessableError
*/
public void addGroup(SearchGroup group) {
public void addGroup(SearchGroup group) throws UnprocessableError {
if (!hasElements()) {
throw new RuntimeException("Operation not allowed for " + getType());
throw new UnprocessableError("Operation not allowed for " + getType());
}
if (isEmpty() || SEARCH_ELEMENTS.LOGICAL_RELATIONS.equals(getTypeOfLastElement())) {
getElements().add(group);
} else {
throw new RuntimeException(
throw new UnprocessableError(
"Wrong search query. A group should be added after a logical relation or a the begining of the query!");
}
}
Expand All @@ -76,18 +80,19 @@ public void addGroup(SearchGroup group) {
* {@link SearchGroup} or a {@link SearchQuery})
*
* @return
* @throws UnprocessableError
*/
public SEARCH_ELEMENTS getTypeOfLastElement() {
public SEARCH_ELEMENTS getTypeOfLastElement() throws UnprocessableError {
SearchElement se = getLastElement();
if (se == null) {
return null;
}
return se.getType();
}

private SearchElement getLastElement() {
private SearchElement getLastElement() throws UnprocessableError {
if (!hasElements()) {
throw new RuntimeException("Operation not allowed for " + getType());
throw new UnprocessableError("Operation not allowed for " + getType());
}
if (!isEmpty()) {
return getElements().get(getElements().size() - 1);
Expand All @@ -97,7 +102,7 @@ private SearchElement getLastElement() {

public boolean isEmpty() {
if (!hasElements()) {
throw new RuntimeException("Operation not allowed for " + getType());
return true;
}
return getElements().size() == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import de.mpg.imeji.exceptions.ImejiException;
import de.mpg.imeji.exceptions.TypeNotAllowedException;
import de.mpg.imeji.exceptions.UnprocessableError;
import de.mpg.imeji.logic.Imeji;
import de.mpg.imeji.logic.controller.resource.ItemController;
import de.mpg.imeji.logic.controller.util.ImejiFactory;
Expand Down Expand Up @@ -178,8 +179,12 @@ public void countDiscardedItems(User user) {
if (getContainer() != null) {
ItemController ic = new ItemController();
SearchQuery q = new SearchQuery();
q.addPair(new SearchPair(SearchFields.status, SearchOperators.EQUALS,
Status.WITHDRAWN.getUriString(), false));
try {
q.addPair(new SearchPair(SearchFields.status, SearchOperators.EQUALS,
Status.WITHDRAWN.getUriString(), false));
} catch (UnprocessableError e) {
LOGGER.error("Error creating query to search for discarded items of a container", e);
}
setSizeDiscarded(
ic.search(getContainer().getId(), q, null, user, null, -1, 0).getNumberOfRecords());
} else {
Expand Down
Loading

0 comments on commit 4d1b7d0

Please sign in to comment.