View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.rule.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 com.google.common.collect.Lists;
28  import fr.ifremer.quadrige3.ui.swing.model.AbstractBeanUIModel;
29  import fr.ifremer.reefdb.dto.configuration.control.RuleListDTO;
30  import fr.ifremer.reefdb.ui.swing.action.AbstractCheckModelAction;
31  import fr.ifremer.reefdb.ui.swing.content.manage.rule.RulesUI;
32  import fr.ifremer.reefdb.ui.swing.content.manage.rule.RulesUIModel;
33  import fr.ifremer.reefdb.ui.swing.content.manage.rule.SaveAction;
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<RulesMenuUIModel, RulesMenuUI, RulesMenuUIHandler> {
43  
44      private List<RuleListDTO> ruleLists;
45  
46      /**
47       * Constructor.
48       *
49       * @param handler Le controller
50       */
51      public SearchAction(final RulesMenuUIHandler 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          final RulesUI rulesUI = getUI().getParentContainer(RulesUI.class);
69          return rulesUI.getHandler();
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      protected boolean isModelModify() {
77          final RulesUIModel model = getLocalModel();
78          return model != null && model.isModify();
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      protected void setModelModify(boolean modelModify) {
86          final RulesUIModel model = getLocalModel();
87          if (model != null) {
88              model.setModify(modelModify);
89          }
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      protected boolean isModelValid() {
97          final RulesUIModel model = getLocalModel();
98          return model == null || model.isValid();
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void doAction() {
106 
107         // Recuperation de la liste des regles
108         if (StringUtils.isNotBlank(getModel().getRuleListCode())) {
109             ruleLists = Lists.newArrayList(getContext().getRuleListService().getRuleList(getModel().getRuleListCode()));
110         } else if (StringUtils.isNotBlank(getModel().getProgramCode())) {
111             ruleLists = Lists.newArrayList(getContext().getRuleListService().getRuleListsForProgram(getModel().getProgramCode()));
112         } else {
113             ruleLists = getContext().getRuleListService().getRuleLists();
114         }
115 
116     }
117 
118     @Override
119     public void postSuccessAction() {
120 
121         getModel().setResults(ruleLists);
122 
123         super.postSuccessAction();
124     }
125 
126     private RulesUIModel getLocalModel() {
127         final RulesUI ui = getUI().getParentContainer(RulesUI.class);
128         if (ui != null) {
129             return ui.getModel();
130         }
131         return null;
132     }
133 
134     @Override
135     protected List<AbstractBeanUIModel> getOtherModelsToModify() {
136         RulesUI ui = getUI().getParentContainer(RulesUI.class).getHandler().getUI();
137         return ImmutableList.of(
138             ui.getRuleListUI().getModel(),
139             ui.getControlProgramTableUI().getModel(),
140             ui.getControlDepartmentTableUI().getModel(),
141             ui.getControlRuleTableUI().getModel(),
142             ui.getControlPmfmTableUI().getModel()
143         );
144     }
145 }