Skip to main content
summaryrefslogtreecommitdiffstats
blob: e82378c8b2c842bed1fb5ea977662c7ae688ef36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2012 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.rest.internal.search;

import java.util.LinkedList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.orcs.data.HasLocalId;
import org.eclipse.osee.orcs.rest.internal.OrcsApplication;
import org.eclipse.osee.orcs.rest.internal.search.dsl.DslFactory;
import org.eclipse.osee.orcs.rest.internal.search.dsl.DslTranslator;
import org.eclipse.osee.orcs.rest.internal.search.dsl.SearchQueryBuilder;
import org.eclipse.osee.orcs.rest.model.search.SearchRequest;
import org.eclipse.osee.orcs.rest.model.search.SearchResponse;
import org.eclipse.osee.orcs.search.QueryBuilder;
import org.eclipse.osee.orcs.search.QueryFactory;

/**
 * @author John R. Misinco
 * @author Roberto E. Escobar
 */
public class ArtifactSearch_V1 extends ArtifactSearch {

   private final SearchQueryBuilder searchQueryBuilder;

   public ArtifactSearch_V1(UriInfo uriInfo, Request request, String branchUuid) {
      super(uriInfo, request, branchUuid);
      searchQueryBuilder = DslFactory.createQueryBuilder();
   }

   /**
    * @param alt The return data format. Possible values:
    * <ul>
    * <li>xml
    * <li>text (not supported)
    * <li>json (not supported)
    * <li>xhtml (not supported)
    * </ul>
    * @param fields What to do with the query. For example, once the search has produced a result set, what aspect of
    * the result set should be returned? Possible values:
    * <ul>
    * <li>count = Return an integer value counting the number of items returned by the query. Note that none of the
    * artifacts or attributes of the search result will be returned to the client. Only a count of the search result
    * items.
    * </ul>
    * @param rawQuery A query string with a custom syntax (see below). The query string syntax must be properly <a
    * href="http://en.wikipedia.org/wiki/Percent-encoding">URL-encoded</a>
    * @param fromTransaction (Optional) Transaction ID for a historical context. Use this parameter if you want the
    * search results to represent a specific point in time in the past. Without this parameter search results will
    * reflect the most current state of the data.
    * @param includeCache (Optional) Boolean parameter that configures the search to ???
    * @param includeDeleted (Optional) Boolean parameter that configures the search to include deleted artifacts in its
    * result set and analysis.
    * @return A {@link SearchResponse} object containing the results of the search, configuration and analysis
    * information.
    * @throws OseeCoreException<br>
    * <br>
    * <b>rawQuery Syntax</b><br>
    * The query syntax is composed of one or more predicates - atomic search instructions - that can be appended
    * together as compound AND or OR query statements. Each predicate is surrounded by square brackets '[' and ']' which
    * makes writing a formal definition of the syntax a little confusing since the square bracket traditionally
    * surrounds optional items in a syntax. Because of this please note in the formal definition below literal square
    * brackets - those which belong as actual characters in the syntax - are escaped with a back slash character '\'.<br>
    * <b>Example Request String</b><br>
    * <FONT FACE="Courier New">
    * http://localhost:8089/oseex/branch/AyH_fDpMERA+zDfML4gA/artifact/search/v1?alt=xml&fields=
    * count&q=[t:attrType%26tp: 123%26op:==%26v:Smith]%26[t:attrType%26tp:456%26op:==%26v:John]</FONT><br>
    * <i>Note: "%26" = "&"</i>
    * <ul>
    * <li><b>http://localhost:8089/oseex/branch/AyH_fDpMERA+zDfML4gA/artifact/search/v1</b> ==> URL address
    * <li><b>alt=xml</b> ==> Search results will be formatted as XML
    * <li><b>fields=count</b> ==> Result returned will be a count of the search results
    * <li><b>q=[...]%26[...]</b> ==> The query string. In this example there are two search predicates "AND"-ed
    * together.
    * <li><b>t:attrType%26tp: 123%26op:==%26v:Smith</b> ==> Presumably "123" is the UUID for the "Last Name" attribute
    * type. This predicate can be translated into English as
    * <b>"Find all the artifacts with a <i>Last Name</i> <i>attribute</i> that equals <i>'Smith'</i>."</b>
    * <li><b>t:attrType%26tp:456%26op:==%26v:John</b> ==> Presumably "456" is the UUID for the "First Name" attribute
    * type. <b>"Find all the artifacts with a <i>First Name</i> <i>attribute</i> that equals <i>'John'</i>."</b>
    * </ul>
    */
   @GET
   @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
   public SearchResponse getSearchWithQueryParams(@QueryParam("alt") String alt, @QueryParam("fields") String fields, @QueryParam("q") String rawQuery, @QueryParam("fromTx") int fromTransaction, @QueryParam("includeDeleted") boolean includeDeleted) throws OseeCoreException {
      DslTranslator translator = DslFactory.createTranslator();
      SearchRequest params =
         new SearchRequest(getBranchUuid(), translator.translate(rawQuery), alt, fields, fromTransaction,
            includeDeleted);
      return search(params);
   }

   @POST
   @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
   @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
   public SearchResponse getSearchWithMatrixParams(SearchRequest parameters) throws OseeCoreException {
      return search(parameters);
   }

   private SearchResponse search(SearchRequest params) throws OseeCoreException {
      long startTime = System.currentTimeMillis();

      QueryFactory qFactory = OrcsApplication.getOrcsApi().getQueryFactory(null); // Fix this

      QueryBuilder builder = searchQueryBuilder.build(qFactory, params);

      builder.includeDeletedArtifacts(params.isIncludeDeleted());

      if (params.getFromTx() > 0) {
         builder.fromTransaction(params.getFromTx());
      }

      SearchResponse result;
      if (params.getFields().equals("count")) {
         result = new SearchResponse();
         int total = builder.getCount();
         result.setTotal(total);

      } else if (params.getFields().equals("ids")) {
         List<Integer> localIds = new LinkedList<Integer>();
         for (HasLocalId art : builder.getResultsAsLocalIds()) {
            localIds.add(art.getLocalId());
         }
         result = new SearchResponse();
         result.setIds(localIds);
         result.setTotal(localIds.size());
      } else {
         throw new UnsupportedOperationException();
      }
      result.setSearchRequest(params);
      result.setSearchTime(System.currentTimeMillis() - startTime);
      return result;
   }
}

Back to the top