Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3eaec3f466a7a644cf0b113b8886bea9e3591a2f (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (c) 2011 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.ats.presenter.internal;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import org.eclipse.osee.ats.ui.api.data.AtsSearchParameters;
import org.eclipse.osee.ats.ui.api.search.AtsArtifactProvider;
import org.eclipse.osee.ats.ui.api.search.AtsSearchPresenter;
import org.eclipse.osee.ats.ui.api.view.AtsSearchHeaderComponent;
import org.eclipse.osee.display.api.components.ArtifactHeaderComponent;
import org.eclipse.osee.display.api.components.AttributeComponent;
import org.eclipse.osee.display.api.components.DisplayOptionsComponent;
import org.eclipse.osee.display.api.components.RelationComponent;
import org.eclipse.osee.display.api.components.SearchResultsListComponent;
import org.eclipse.osee.display.api.data.ViewId;
import org.eclipse.osee.display.api.search.SearchNavigator;
import org.eclipse.osee.display.presenter.SearchPresenterImpl;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.UrlQuery;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.data.ArtifactReadable;

/**
 * @author John R. Misinco
 */
public class AtsSearchPresenterImpl<T extends AtsSearchHeaderComponent, K extends AtsSearchParameters> extends SearchPresenterImpl<T, K> implements AtsSearchPresenter<T, K> {

   private final AtsArtifactProvider atsArtifactProvider;

   public AtsSearchPresenterImpl(AtsArtifactProvider artifactProvider, Log logger) {
      super(artifactProvider, logger);
      atsArtifactProvider = artifactProvider;
   }

   @Override
   public void selectSearch(String url, K params, SearchNavigator atsNavigator) {
      String newUrl = encode("", params, null);
      atsNavigator.navigateSearchResults(newUrl);
   }

   private void addProgramsToSearchHeader(T headerComponent) {
      headerComponent.clearAll();
      Collection<ViewId> programs = null;
      try {
         programs = getPrograms();
      } catch (Exception ex) {
         setErrorMessage(headerComponent, "Error in addProgramsToSearchHeader", ex);
         return;
      }
      for (ViewId program : programs) {
         headerComponent.addProgram(program);
      }
   }

   @Override
   public void initSearchResults(String url, T searchHeaderComponent, SearchResultsListComponent resultsComponent, DisplayOptionsComponent optionsComponent) {
      setSearchHeaderFields(url, searchHeaderComponent);
      resultsComponent.clearAll();

      if (!Strings.isValid(url)) {
         sendSearchCompleted();
         return;
      }

      AtsSearchParameters params = decodeIt(url);

      if (params == null || !params.isValid()) {
         return;
      }

      Long branchUuid;
      try {
         branchUuid = atsArtifactProvider.getBaselineBranchUuid(params.getBuild().getGuid());
      } catch (Exception ex) {
         setErrorMessage(searchHeaderComponent, "Error in initSearchResults", ex);
         return;
      }

      if (branchUuid == null) {
         setErrorMessage(resultsComponent, "Could not find baseline branch uuid for selected build/program", null);
      } else {
         String newUrl = encode(url, params, String.valueOf(branchUuid));
         super.initSearchResults(newUrl, searchHeaderComponent, resultsComponent, optionsComponent);
      }
   }

   @Override
   public void selectProgram(ViewId program, T headerComponent) {
      headerComponent.clearBuilds();
      Collection<ViewId> builds = null;
      if (program != null) {
         try {
            builds = getBuilds(program);
         } catch (Exception ex) {
            setErrorMessage(headerComponent, "Error in selectProgram", ex);
            return;
         }
         for (ViewId build : builds) {
            headerComponent.addBuild(build);
         }
      }
   }

   protected Collection<ViewId> getPrograms() throws OseeCoreException {
      Collection<ViewId> toReturn = new LinkedList<ViewId>();
      Iterable<ArtifactReadable> programs = atsArtifactProvider.getPrograms();
      if (programs != null) {
         for (ArtifactReadable program : programs) {
            toReturn.add(new ViewId(program.getGuid(), program.getName()));
         }
      }
      return toReturn;
   }

   protected Collection<ViewId> getBuilds(ViewId program) throws OseeCoreException {
      Iterable<ArtifactReadable> relatedBuilds = atsArtifactProvider.getBuilds(program.getGuid());
      Collection<ViewId> builds = new ArrayList<ViewId>();
      if (relatedBuilds != null) {
         for (ArtifactReadable build : relatedBuilds) {
            builds.add(new ViewId(build.getGuid(), build.getName()));
         }
      }
      return builds;
   }

   protected String encode(String url, AtsSearchParameters searchParams, String branchId) {
      UrlQuery query = new UrlQuery();
      try {
         query.parse(url);

         if (Strings.isValid(branchId)) {
            query.putInPlace("branch", branchId);
         }

         query.putInPlace("program", searchParams.getProgram().getGuid());
         query.putInPlace("build", searchParams.getBuild().getGuid());
         query.putInPlace("nameOnly", String.valueOf(searchParams.isNameOnly()));
         query.putInPlace("search", searchParams.getSearchString());

         return "/" + query.toString();
      } catch (UnsupportedEncodingException ex) {
         logger.error(ex, "Error in encode");
         return "";
      }
   }

   protected AtsSearchParameters decodeIt(String url) {
      UrlQuery query = new UrlQuery();
      try {
         query.parse(url);
      } catch (UnsupportedEncodingException ex) {
         logger.error(ex, "Error in encode");
         return null;
      }

      ViewId program = null, build = null;

      if (query.containsKey("program")) {
         program = new ViewId(query.getParameter("program"), "");
      }

      if (query.containsKey("build")) {
         build = new ViewId(query.getParameter("build"), "");
      }

      String nValue = query.getParameter("nameOnly");
      boolean nameOnly = nValue == null ? false : nValue.equalsIgnoreCase("true");
      String searchPhrase = "";
      if (query.containsKey("search")) {
         searchPhrase = query.getParameter("search");
      }

      //      String vValue = data.get("verbose");
      //      boolean verbose = vValue == null ? false : vValue.equalsIgnoreCase("true");
      return new AtsSearchParameters(searchPhrase, nameOnly, build, program);
   }

   protected void setSearchHeaderFields(String url, T searchHeaderComp) {
      searchHeaderComp.clearAll();
      addProgramsToSearchHeader(searchHeaderComp);

      if (!Strings.isValid(url)) {
         return;
      }

      AtsSearchParameters params = decodeIt(url);
      if (!params.isValid()) {
         return;
      }

      selectProgram(params.getProgram(), searchHeaderComp);
      searchHeaderComp.setSearchCriteria(params);

   }

   @Override
   public void initArtifactPage(String url, T searchHeaderComp, ArtifactHeaderComponent artHeaderComp, RelationComponent relComp, AttributeComponent attrComp, DisplayOptionsComponent options) {
      setSearchHeaderFields(url, searchHeaderComp);
      super.initArtifactPage(url, searchHeaderComp, artHeaderComp, relComp, attrComp, options);
   }

}

Back to the top