View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.program.menu;
2   
3   /*
4    * #%L
5    * Reef DB :: 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.quadrige3.core.security.SecurityContextHelper;
28  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
29  import fr.ifremer.reefdb.dto.configuration.programStrategy.ProgramDTO;
30  import fr.ifremer.reefdb.service.StatusFilter;
31  import fr.ifremer.reefdb.service.administration.program.ProgramStrategyService;
32  import fr.ifremer.reefdb.ui.swing.action.AbstractCheckModelAction;
33  import fr.ifremer.reefdb.ui.swing.content.manage.program.ProgramsUI;
34  import fr.ifremer.reefdb.ui.swing.content.manage.program.ProgramsUIModel;
35  import fr.ifremer.reefdb.ui.swing.content.manage.program.SaveAction;
36  import org.apache.commons.collections4.CollectionUtils;
37  import org.nuiton.jaxx.application.swing.AbstractApplicationUIHandler;
38  
39  import java.util.List;
40  import java.util.stream.Collectors;
41  
42  /**
43   * Search action.
44   */
45  public class SearchAction extends AbstractCheckModelAction<ProgramsMenuUIModel, ProgramsMenuUI, ProgramsMenuUIHandler> {
46  
47      private List<ProgramDTO> results;
48  
49      /**
50       * Constructor.
51       *
52       * @param handler Le controller
53       */
54      public SearchAction(final ProgramsMenuUIHandler handler) {
55          super(handler, false);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      protected Class<SaveAction> getSaveActionClass() {
61          return SaveAction.class;
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      protected AbstractApplicationUIHandler<?, ?> getSaveHandler() {
67          return getHandler().getProgramsUI().getHandler();
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      protected boolean isModelModify() {
73          final ProgramsUIModel model = getLocalModel();
74          return model != null && model.isModify();
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      protected void setModelModify(boolean modelModify) {
80          final ProgramsUIModel model = getLocalModel();
81          if (model != null) {
82              model.setModify(modelModify);
83          }
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      protected boolean isModelValid() {
89          final ProgramsUIModel model = getLocalModel();
90          return model == null || model.isValid();
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public boolean prepareAction() throws Exception {
96  
97          // Suppression des informations du contexte
98          getContext().setSelectedProgramCode(null);
99          getContext().setSelectedLocationId(null);
100 
101         return super.prepareAction();
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public void doAction() {
107 
108         ProgramStrategyService service = getContext().getProgramStrategyService();
109         int userId = SecurityContextHelper.getQuadrigeUserId();
110         StatusFilter statusFilter = StatusFilter.toActiveLocalOrNational(getModel().getLocal());
111         List<ProgramDTO> programs;
112 
113         if (getModel().isOnlyManagedPrograms()) {
114 
115             // get managed programs only
116             programs = service.getManagedProgramsByUserAndStatus(userId, statusFilter);
117 
118         } else {
119 
120             // get writable programs only
121             programs = service.getWritableProgramsByUserAndStatus(userId, statusFilter);
122 
123         }
124 
125         results = null;
126         if (CollectionUtils.isNotEmpty(programs)) {
127             if (getModel().getProgram() != null) {
128                 // filter by selected program in combo
129                 results = programs.stream().filter(program -> program.equals(getModel().getProgram())).collect(Collectors.toList());
130             } else {
131                 results = programs;
132             }
133         }
134 
135     }
136 
137     /** {@inheritDoc} */
138     @Override
139     public void postSuccessAction() {
140 
141         getModel().setResults(results);
142 
143         super.postSuccessAction();
144     }
145 
146     private ProgramsUIModel getLocalModel() {
147         ProgramsUI ui = getHandler().getProgramsUI();
148         return ui != null ? ui.getModel() : null;
149     }
150 
151     @Override
152     protected List<AbstractBeanUIModel> getOtherModelsToModify() {
153         ProgramsUI ui = getHandler().getProgramsUI();
154         if (ui != null) {
155             return ImmutableList.of(
156                 ui.getProgramsTableUI().getModel(),
157                 ui.getLocationsTableUI().getModel(),
158                 ui.getPmfmsTableUI().getModel(),
159                 ui.getStrategiesTableUI().getModel()
160             );
161         }
162         return super.getOtherModelsToModify();
163     }
164 }