View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.program.menu;
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.configuration.programStrategy.ProgramDTO;
28  import fr.ifremer.dali.ui.swing.action.AbstractCheckModelAction;
29  import fr.ifremer.dali.ui.swing.content.manage.program.ProgramsUI;
30  import fr.ifremer.dali.ui.swing.content.manage.program.ProgramsUIModel;
31  import fr.ifremer.dali.ui.swing.content.manage.program.SaveAction;
32  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
33  import org.apache.commons.collections4.CollectionUtils;
34  import org.apache.commons.lang3.StringUtils;
35  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
36  
37  import java.util.List;
38  
39  /**
40   * Search action.
41   */
42  public class SearchAction extends AbstractCheckModelAction<ProgramsMenuUIModel, ProgramsMenuUI, ProgramsMenuUIHandler> {
43  
44      private List<ProgramDTO> results;
45  
46      /**
47       * Constructor.
48       *
49       * @param handler Le controller
50       */
51      public SearchAction(final ProgramsMenuUIHandler handler) {
52          super(handler, false);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      protected Class<SaveAction> getSaveActionClass() {
60          return SaveAction.class;
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      protected AbstractApplicationUIHandler<?, ?> getSaveHandler() {
68          return getHandler().getProgramsUI().getHandler();
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      protected boolean isModelModify() {
76          final ProgramsUIModel model = getLocalModel();
77          return model != null && model.isModify();
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      protected void setModelModify(boolean modelModify) {
85          final ProgramsUIModel model = getLocalModel();
86          if (model != null) {
87              model.setModify(modelModify);
88          }
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      protected boolean isModelValid() {
96          final ProgramsUIModel model = getLocalModel();
97          return model == null || model.isValid();
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public boolean prepareAction() throws Exception {
105 
106         return super.prepareAction();
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public void doAction() throws Exception {
114 
115         results = StringUtils.isBlank(getModel().getProgramCode())
116                 ? getContext().getProgramStrategyService().getWritablePrograms()
117                 : ImmutableList.of(getContext().getProgramStrategyService().getWritableProgramByCode(getModel().getProgramCode()));
118 
119         // remove errors (cause they are stored in cache)
120         if (CollectionUtils.isNotEmpty(results))
121             results.forEach(program -> program.getErrors().clear());
122 
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public void postSuccessAction() {
130 
131         getModel().setResults(results);
132 
133         super.postSuccessAction();
134     }
135 
136     private ProgramsUIModel getLocalModel() {
137         ProgramsUI ui = getHandler().getProgramsUI();
138         return ui != null ? ui.getModel() : null;
139     }
140 
141     @Override
142     protected List<AbstractBeanUIModel> getOtherModelsToModify() {
143         ProgramsUI ui = getHandler().getProgramsUI();
144         if (ui != null) {
145             return ImmutableList.of(
146                     ui.getProgramsTableUI().getModel(),
147                     ui.getLocationsTableUI().getModel(),
148                     ui.getPmfmsTableUI().getModel(),
149                     ui.getStrategiesTableUI().getModel()
150             );
151         }
152         return super.getOtherModelsToModify();
153     }
154 
155 }