View Javadoc
1   /*
2    * #%L
3    * Coser :: Web
4    * %%
5    * Copyright (C) 2011 Ifremer, Codelutin, Chatellier Eric
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Affero General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   * 
17   * You should have received a copy of the GNU Affero General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   * #L%
20   */
21  
22  package fr.ifremer.coser.web.actions.admin;
23  
24  import com.google.common.collect.Maps;
25  import fr.ifremer.coser.result.ResultType;
26  import fr.ifremer.coser.result.repository.ResultRepositoryType;
27  import fr.ifremer.coser.result.request.GetAllResultsRequest;
28  import fr.ifremer.coser.web.CoserWebException;
29  import fr.ifremer.coser.web.actions.common.AbstractCoserAction;
30  import org.apache.commons.collections4.MapUtils;
31  import org.apache.commons.lang3.StringUtils;
32  
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Set;
36  
37  /**
38   * Project list action.
39   *
40   * Last update : $Date$
41   * By : $Author$
42   *
43   * @author chatellier
44   * @version $Revision$
45   */
46  public class ListProjectsAction extends AbstractCoserAction {
47  
48      /** serialVersionUID. */
49      private static final long serialVersionUID = 6024588562104111883L;
50  
51      // All types of result repository
52      protected Map<String, ResultRepositoryType> repositoryTypes;
53  
54      // Selected result repository type
55      protected String selectedRepositoryType;
56  
57      protected Map<String, String> facades;
58  
59      protected Map<String, List<String>> zonesByFacades;
60  
61      protected Map<ResultType, Map<String, String>> results;
62  
63      protected Map<String, String> indicatorsResults;
64  
65      protected Map<String, String> mapsResults;
66  
67      public Map<String, ResultRepositoryType> getRepositoryTypes() {
68          return repositoryTypes;
69      }
70  
71      public String getSelectedRepositoryType() {
72          return selectedRepositoryType;
73      }
74  
75      public void setSelectedRepositoryType(String selectedRepositoryType) {
76          this.selectedRepositoryType = selectedRepositoryType;
77      }
78  
79      public Map<String, String> getFacades() {
80          return facades;
81      }
82  
83      public Map<String, List<String>> getZonesByFacades() {
84          return zonesByFacades;
85      }
86  
87      public Map<String, String> getResults(ResultType resultType) {
88          return results.get(resultType);
89      }
90  
91      public String getZoneDisplayName(String zoneId) {
92          return getService().getZoneFullName(zoneId);
93      }
94  
95      @Override
96      public String execute() {
97  
98          repositoryTypes = getService().getRepositoryTypes();
99          if (MapUtils.isEmpty(repositoryTypes)) {
100             throw new CoserWebException("No result repository type defined!");
101         }
102 
103         if (StringUtils.isEmpty(selectedRepositoryType)) {
104 
105             // use first value
106             ResultRepositoryType repositoryType = repositoryTypes.values().iterator().next();
107             selectedRepositoryType = repositoryType.getId();
108         }
109 
110         facades = getService().getFacades();
111         zonesByFacades = getService().getZoneByFacade();
112 
113         GetAllResultsRequest request = requestBuilder(GetAllResultsRequest.class).
114                 addRepositoryType(getSelectedRepositoryType()).
115                 toRequest();
116 
117         results = Maps.newEnumMap(ResultType.class);
118         if (selectedRepositoryType != null) {
119             ResultRepositoryType repositoryType = repositoryTypes.get(selectedRepositoryType);
120             Set<ResultType> resultTypes = repositoryType.getResultTypes();
121             for (ResultType resultType : resultTypes) {
122 
123                 // get projects for this type
124                 request.setResultType(resultType);
125                 Map<String, String> resultsForType = getService().toMap(request);
126                 results.put(resultType, resultsForType);
127             }
128         }
129 
130         return SUCCESS;
131     }
132 
133 //    public Map<String, String> getIndicatorsResults() {
134 //        return indicatorsResults;
135 //    }
136 //
137 //    public Map<String, String> getMapsResults() {
138 //        return mapsResults;
139 //    }
140 //
141 //    public String getZoneDisplayName(String zoneId) {
142 //        WebService webService = ServiceFactory.getWebService();
143 //        String zoneDisplayName = null;
144 //        try {
145 //            zoneDisplayName = webService.getZoneFullName(zoneId);
146 //        } catch (CoserBusinessException ex) {
147 //            throw new CoserWebException("Can't get zone name", ex);
148 //        }
149 //        return zoneDisplayName;
150 //    }
151 //
152 //    public String execute() {
153 //        WebService webService = ServiceFactory.getWebService();
154 //
155 //        try {
156 //            facades = webService.getFacades();
157 //            zonesByFacades = webService.getZoneByFacade();
158 //            indicatorsResults = webService.getIndicatorsResultsPerZone();
159 //            mapsResults = webService.getMapsResultsPerZone();
160 //        } catch (CoserBusinessException ex) {
161 //            throw new CoserWebException("Can't get data from web service", ex);
162 //        }
163 //
164 //        return SUCCESS;
165 //    }
166 }