View Javadoc
1   /*
2    * #%L
3    * Coser :: Web
4    * %%
5    * Copyright (C) 2010 - 2012 Ifremer, Codelutin, Chatellier Eric, Chemit Tony
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.com;
23  
24  import fr.ifremer.coser.result.result.FileResult;
25  import fr.ifremer.coser.result.request.GetCommunityIndicatorResultDataRequest;
26  import fr.ifremer.coser.result.request.GetSpeciesListForCommunityIndicatorResultRequest;
27  import fr.ifremer.coser.web.actions.common.AbstractCoserJspAction;
28  import org.apache.commons.collections4.MapUtils;
29  import org.apache.commons.lang3.StringUtils;
30  import org.apache.struts2.convention.annotation.Action;
31  import org.apache.struts2.convention.annotation.Result;
32  
33  import java.io.InputStream;
34  import java.util.Map;
35  
36  /**
37   * Télécharge les données qui ont servi a généré le graph au format CSV.
38   * <p/>
39   * Parametre : zone, indicator.
40   *
41   * @author chatellier
42   * @version $Revision$
43   *          <p/>
44   *          Last update : $Date$
45   *          By : $Author$
46   */
47  public class GraphDownloadAction extends AbstractCoserJspAction {
48  
49      /** serialVersionUID. */
50      private static final long serialVersionUID = 3385467755357775199L;
51  
52      protected String facade;
53  
54      protected String zone;
55  
56      protected String indicator;
57  
58      /** La liste actuellement selectionnée (cas null géré). */
59      protected String list;
60  
61      protected FileResult result;
62  
63      public void setFacade(String facade) {
64          this.facade = facade;
65      }
66  
67      public String getZone() {
68          return zone;
69      }
70  
71      public void setZone(String zone) {
72          this.zone = zone;
73      }
74  
75      public String getIndicator() {
76          return indicator;
77      }
78  
79      public void setIndicator(String indicator) {
80          this.indicator = indicator;
81      }
82  
83      public String getList() {
84          return list;
85      }
86  
87      public void setList(String list) {
88          this.list = list;
89      }
90  
91      public String getSource() {
92          return result.getSource();
93      }
94  
95      @Action(results = {@Result(type = "stream", params = {"contentType", "application/zip", "contentDisposition", "attachment; filename=\"${filename}\""})})
96      public String execute() {
97  
98          GetCommunityIndicatorResultDataRequest request =
99                  requestBuilder(GetCommunityIndicatorResultDataRequest.class).
100                         addFacade(facade).
101                         addZone(zone).
102                         addIndicator(indicator).
103                         addSpecies(list).
104                         toRequest();
105 
106         if (StringUtils.isEmpty(list)) {
107 
108             // Get the first species found in indicators file
109             GetSpeciesListForCommunityIndicatorResultRequest speciesRequest =
110                     requestBuilder(GetSpeciesListForCommunityIndicatorResultRequest.class).
111                             addFacade(facade).
112                             addZone(zone).
113                             addIndicator(indicator).
114                             toRequest();
115             Map<String, String> speciesMap = getService().toMap(speciesRequest);
116             if (MapUtils.isNotEmpty(speciesMap)) {
117                 request.setSpecies(speciesMap.keySet().iterator().next());
118             }
119         }
120 
121         result = getService().toFileResult(request);
122         return SUCCESS;
123     }
124 
125     public String getFilename() {
126         return indicator + ".zip";
127     }
128 
129     public InputStream getInputStream() {
130         InputStream input = result.getInputStream();
131         return input;
132     }
133 
134 //    public InputStream getInputStream() {
135 //        WebService webService = ServiceFactory.getWebService();
136 //
137 //        Locale locale = getLocale();
138 //
139 //        InputStream input = null;
140 //        try {
141 //            File mapImage = webService.getChartData(zone, null, indicator, list, locale);
142 //            input = new FileInputStream(mapImage);
143 //        } catch (Exception ex) {
144 //            if (log.isErrorEnabled()) {
145 //                log.error("Can't get file data", ex);
146 //            }
147 //            throw new CoserWebException("Can't get map file", ex);
148 //        }
149 //
150 //        return input;
151 //    }
152 }