View Javadoc
1   package fr.ifremer.dali.ui.swing.content.home;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.ImmutableList;
27  import fr.ifremer.dali.dto.DaliBeanFactory;
28  import fr.ifremer.dali.dto.data.survey.SurveyDTO;
29  import fr.ifremer.dali.dto.data.survey.SurveyFilterDTO;
30  import fr.ifremer.dali.ui.swing.action.AbstractCheckModelAction;
31  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
32  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
33  
34  import java.util.List;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Action permettant la recherche des obervations.
40   */
41  public class SearchAction extends AbstractCheckModelAction<HomeUIModel, HomeUI, HomeUIHandler> {
42  
43      private List<SurveyDTO> surveys;
44      private boolean controlAfterSelect = false;
45  
46      /**
47       * Constructor.
48       *
49       * @param handler Le controller
50       */
51      public SearchAction(final HomeUIHandler handler) {
52          super(handler, false);
53          setActionDescription(t("dali.action.search.tip"));
54      }
55  
56      public void setControlAfterSelect(boolean controlAfterSelect) {
57          this.controlAfterSelect = controlAfterSelect;
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      protected Class<SaveAction> getSaveActionClass() {
63          return SaveAction.class;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      protected AbstractApplicationUIHandler<?, ?> getSaveHandler() {
69          return getHandler();
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      protected boolean isModelModify() {
75          return getModel().isModify();
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      protected void setModelModify(boolean modelModify) {
81          getModel().setModify(modelModify);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      protected boolean isModelValid() {
87          return getModel().isValid();
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public void doAction() throws Exception {
93  
94          // Tell surveys table handler a load has begun
95          getUI().getSurveysTableUI().getHandler().beforeLoad();
96  
97          // Create new search parameters
98          final SurveyFilterDTO surveyFilter = DaliBeanFactory.newSurveyFilterDTO();
99  
100         // Gather model properties
101         surveyFilter.setCampaignId(getModel().getCampaignId());
102         surveyFilter.setProgramCode(getModel().getProgramCode());
103         surveyFilter.setLocationId(getModel().getLocationId());
104         surveyFilter.setDate1(getModel().getStartDate());
105         surveyFilter.setDate2(getModel().getEndDate());
106         surveyFilter.setSearchDateId(getModel().getSearchDateId());
107         surveyFilter.setName(getModel().getName());
108         surveyFilter.setComment(getModel().getComment());
109         surveyFilter.setStateId(getModel().getStateId());
110         surveyFilter.setShareId(getModel().getSynchronizationStatusId());
111 
112         // Save into context
113         getContext().setSurveyFilter(surveyFilter);
114 
115         // Get observations
116         surveys = getContext().getObservationService().getSurveys(surveyFilter);
117     }
118 
119     @Override
120     protected List<AbstractBeanUIModel> getOtherModelsToModify() {
121         return ImmutableList.of(
122                 getModel().getSurveysTableUIModel(),
123                 getModel().getOperationsTableUIModel()
124         );
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public void postSuccessAction() {
130         super.postSuccessAction();
131 
132         getUI().getSurveysTableUI().getSurveysTable().clearSelection();
133 
134         getModel().getSurveysTableUIModel().setBeans(surveys);
135 
136         // The load is done
137         getUI().getSurveysTableUI().getHandler().afterLoad();
138 
139         if (getContext().getSelectedSurveyId() != null) {
140 
141             // Select
142             getUI().getSurveysTableUI().getHandler().selectRowById(getContext().getSelectedSurveyId());
143 
144             if (controlAfterSelect) {
145                 getContext().getRulesControlService().controlSurvey(getModel().getSelectedSurvey(), false, false);
146                 getUI().getSurveysTableUI().getHandler().ensureColumnsWithErrorAreVisible(getModel().getSelectedSurvey());
147             }
148         }
149 
150     }
151 
152     @Override
153     protected void releaseAction() {
154         super.releaseAction();
155         surveys = null;
156         controlAfterSelect = false;
157     }
158 }