Skip to content
Snippets Groups Projects
Commit 0b8a49b6 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api allows to retrieve elements by query

parent 8bc4f9f2
No related branches found
No related tags found
1 merge request!5Frontend refactor
......@@ -43,7 +43,7 @@ public class ProjectController extends BaseController {
produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getClosestElementsByCoordinates(@RequestParam(value = "projectId") String projectId,
@RequestParam(value = "modelId") String modelId, @RequestParam(value = "token") String token, @RequestParam(value = "coordinates") String coordinates,
@RequestParam(value = "count", defaultValue = "5") Integer count) throws QueryException, SecurityException {
@RequestParam(value = "count", defaultValue = "5") String count) throws QueryException, SecurityException {
String[] tmp = coordinates.split(",");
if (tmp.length != 2) {
throw new QueryException("Coordinates must be in the format: 'xxx.xx,yyy.yy'");
......@@ -60,4 +60,11 @@ public class ProjectController extends BaseController {
return projectController.getClosestElementsByCoordinates(projectId, modelId, token, pointCoordinates, Integer.valueOf(count));
}
@RequestMapping(value = "/getElementsByQuery", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getElementsByQuery(@RequestParam(value = "projectId") String projectId, @RequestParam(value = "token") String token,
@RequestParam(value = "query") String query, @RequestParam(value = "maxElements", defaultValue = "100") Integer maxElements,
@RequestParam(value = "perfectMatch", defaultValue = "false") String perfectMatch) throws QueryException, SecurityException {
return projectController.getElementsByQuery(projectId, token, query, maxElements, perfectMatch);
}
}
\ No newline at end of file
......@@ -113,24 +113,29 @@ public class ProjectRestImpl {
List<Object> elements = searchService.getClosestElements(submodel, coordinates, count);
for (Object object : elements) {
Map<String, Object> result = new HashMap<>();
if (object instanceof Element) {
result.put("type", ElementIdentifierType.ALIAS);
Element element = (Element) object;
result.put("id", element.getId());
result.put("modelId", element.getModel().getId());
} else if (object instanceof Reaction) {
result.put("type", ElementIdentifierType.REACTION);
Reaction element = (Reaction) object;
result.put("id", element.getId());
result.put("modelId", element.getModel().getId());
} else {
throw new InvalidStateException("Unknown type of result: " + object.getClass());
}
Map<String, Object> result = createMinifiedSearchResult(object);
resultMap.add(result);
}
return resultMap;
}
private Map<String, Object> createMinifiedSearchResult(Object object) {
Map<String, Object> result = new HashMap<>();
if (object instanceof Element) {
result.put("type", ElementIdentifierType.ALIAS);
Element element = (Element) object;
result.put("id", element.getId());
result.put("modelId", element.getModel().getId());
} else if (object instanceof Reaction) {
result.put("type", ElementIdentifierType.REACTION);
Reaction element = (Reaction) object;
result.put("id", element.getId());
result.put("modelId", element.getModel().getId());
} else {
throw new InvalidStateException("Unknown type of result: " + object.getClass());
}
return result;
};
public List<Map<String, Object>> getReactions(String projectId, String id, String columns, String token) throws UserAccessException, SecurityException {
......@@ -323,4 +328,19 @@ public class ProjectRestImpl {
this.searchService = searchService;
}
public List<Map<String, Object>> getElementsByQuery(String projectId, String token, String query, Integer maxElements, String perfectMatch) throws SecurityException {
List<Map<String, Object>> resultMap = new ArrayList<>();
Model model = modelService.getLastModelByProjectId(projectId, userService.getToken(token));
Integer limit = Integer.valueOf(maxElements);
boolean match = perfectMatch.equals("true");
List<Object> elements = searchService.searchByQuery(model, query, limit, match);
for (Object object : elements) {
Map<String, Object> result = createMinifiedSearchResult(object);
resultMap.add(result);
}
return resultMap;
}
}
......@@ -32,7 +32,7 @@ public class CommentRestImplTest extends RestTestFunctions {
@Test
public void testGetForNonExisting() throws Exception {
try {
commentRestImpl.getCommentList(token.getId(), "unk", "","","");
commentRestImpl.getCommentList(token.getId(), "unk", "","","","");
fail("Exception expected");
} catch (QueryException e2) {
assertTrue(e2.getMessage().contains("Project with given id doesn't exist"));
......
......@@ -67,47 +67,47 @@ public class SearchService implements ISearchService {
/**
* Prefix used in search by name interface to limit results only to species.
*/
public static final String SPECIES_SEARCH_PREFIX = "species";
public static final String SPECIES_SEARCH_PREFIX = "species";
/**
* Prefix used in search by name interface to limit results only to reactions.
*/
public static final String REACTION_SEARCH_PREFIX = "reaction";
public static final String REACTION_SEARCH_PREFIX = "reaction";
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(SearchService.class.getName());
private static Logger logger = Logger.getLogger(SearchService.class.getName());
/**
* Data access object for models.
*/
@Autowired
private ModelDao modelDao;
private ModelDao modelDao;
/**
* Service used for managing search history.
*/
@Autowired
private ISearchHistoryService searchHistoryService;
private ISearchHistoryService searchHistoryService;
/**
* Factory object used to create {@link FullAliasView} objects.
*/
@Autowired
private FullAliasViewFactory fullAliasViewFactory;
private FullAliasViewFactory fullAliasViewFactory;
/**
* Factory object used to create {@link FullReactionView} objects.
*/
@Autowired
private FullReactionViewFactory fullReactionViewFactory;
private FullReactionViewFactory fullReactionViewFactory;
/**
* Object used for indexing elements on the map.
*/
private SearchIndexer searchIndexer = new SearchIndexer();
private SearchIndexer searchIndexer = new SearchIndexer();
/**
* This object maps class of elements on the map into short string prefixes
......@@ -775,4 +775,22 @@ public class SearchService implements ISearchService {
}
}
@Override
public List<Object> searchByQuery(Model model, String query, int limit, Boolean perfectMatch) {
SearchElementResult resultElement = searchByQuery(model, query, limit, perfectMatch, null);
List<Object> result = new ArrayList<>();
for (IHeavyView view : resultElement.getElements()) {
if (view instanceof FullAliasView) {
FullAliasView elementView = (FullAliasView) view;
Element element = model.getSubmodelById(elementView.getModelId()).getElementByDbId(Integer.valueOf(elementView.getIdObject()));
result.add(element);
} else if (view instanceof FullReactionView) {
FullReactionView reactionView = (FullReactionView) view;
Reaction element = model.getSubmodelById(reactionView.getModelId()).getReactionByDbId(Integer.valueOf(reactionView.getIdObject()));
result.add(element);
}
}
return result;
}
}
\ No newline at end of file
......@@ -282,6 +282,8 @@ public interface ISearchService {
*/
SearchElementResult searchByQuery(Model model, String query, int limit, Boolean perfectMatch, String ipAddress);
List<Object> searchByQuery(Model model, String query, int limit, Boolean perfectMatch);
/**
* Returns the closest elements to the coordinates on the model.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment