View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.validation;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.ImmutableSet;
26  import fr.ifremer.tutti.persistence.entities.data.Cruise;
27  import fr.ifremer.tutti.persistence.entities.data.Cruises;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.jdesktop.beans.AbstractSerializableBean;
32  import org.nuiton.validator.NuitonValidatorResult;
33  
34  import java.util.HashMap;
35  import java.util.LinkedHashMap;
36  import java.util.Map;
37  import java.util.Set;
38  
39  /**
40   * @author Kevin Morin - kmorin@codelutin.com
41   * @since 1.4
42   */
43  public class ValidateCruiseUIModel extends AbstractSerializableBean {
44  
45      private static final long serialVersionUID = 1L;
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(ValidateCruiseUIModel.class);
49  
50      public static final String PROPERTY_SELECTED_FISHING_OPERATION = "selectedFishingOperation";
51  
52      public static final String PROPERTY_SELECTED_CRUISE = "selectedCruise";
53  
54      public static final String PROPERTY_READY_TO_SYNCH = "readyToSynch";
55  
56      protected FishingOperation selectedFishingOperation;
57  
58      protected final LinkedHashMap<FishingOperation, NuitonValidatorResult> validator = new LinkedHashMap<>();
59  
60      protected NuitonValidatorResult cruiseValidatorResult;
61  
62      protected boolean readyToSynch;
63  
64      protected Cruise cruise;
65  
66      private Cruise selectedCruise;
67      private Object lastSelectedObject;
68  
69      public FishingOperation getSelectedFishingOperation() {
70          return selectedFishingOperation;
71      }
72  
73      public void setSelectedFishingOperation(FishingOperation selectedFishingOperation) {
74          this.selectedFishingOperation = selectedFishingOperation;
75          this.lastSelectedObject= selectedFishingOperation;
76          firePropertyChange(PROPERTY_SELECTED_FISHING_OPERATION, null/** always force to propagate the event **/, selectedFishingOperation);
77      }
78  
79      public Cruise getSelectedCruise() {
80          return selectedCruise;
81      }
82  
83      public void setSelectedCruise(Cruise selectedCruise) {
84          this.selectedCruise = selectedCruise;
85          this.lastSelectedObject= selectedCruise;
86          firePropertyChange(PROPERTY_SELECTED_CRUISE, null /** always force to propagate the event **/, selectedCruise);
87      }
88  
89      public Object getLastSelectedObject() {
90          return lastSelectedObject;
91      }
92  
93      public Map<FishingOperation, NuitonValidatorResult> getValidator() {
94          return new HashMap<>(validator);
95      }
96  
97      public Set<FishingOperation> getFishingOperations() {
98          return ImmutableSet.copyOf(validator.keySet());
99      }
100 
101     public NuitonValidatorResult getValidatorResult(FishingOperation fishingOperation) {
102         return validator.get(fishingOperation);
103     }
104 
105     public NuitonValidatorResult getCruiseValidatorResult() {
106         return cruiseValidatorResult;
107     }
108 
109     public void setCruiseValidatorResult(NuitonValidatorResult cruiseValidatorResult) {
110         this.cruiseValidatorResult = cruiseValidatorResult;
111     }
112 
113     public void addFishingOperationValidatorResults(Map<FishingOperation, NuitonValidatorResult> validatorResultMap) {
114         for (Map.Entry<FishingOperation, NuitonValidatorResult> entry : validatorResultMap.entrySet()) {
115             this.validator.put(entry.getKey(), entry.getValue());
116         }
117     }
118 
119     public void addValidatorResult(FishingOperation fishingOperation, NuitonValidatorResult validatorResult) {
120         validator.put(fishingOperation, validatorResult);
121     }
122 
123     public void computeReadyToSynch() {
124 //        boolean result = Cruises.isDirty(cruise);
125         boolean result = true;
126 //        if (result) {
127         if (cruiseValidatorResult != null) {
128             if (cruiseValidatorResult.hasErrorMessagess()) {
129                 result = false;
130                 if (log.isInfoEnabled()) {
131                     log.info("there is some errors in cruise: " + cruiseValidatorResult.getFieldsForError());
132                 }
133             }
134             if (cruiseValidatorResult.hasFatalMessages()) {
135                 result = false;
136                 if (log.isInfoEnabled()) {
137                     log.info("there is some fatal errors in cruise: " + cruiseValidatorResult.getFieldsForFatal());
138                 }
139             }
140         }
141 
142         for (NuitonValidatorResult nuitonValidatorResult : validator.values()) {
143             if (nuitonValidatorResult.hasErrorMessagess()) {
144                 result = false;
145                 if (log.isInfoEnabled()) {
146                     log.info("there is some errors in fishing operation: " + nuitonValidatorResult.getFieldsForError());
147                 }
148                 break;
149             }
150             if (nuitonValidatorResult.hasFatalMessages()) {
151                 result = false;
152                 if (log.isInfoEnabled()) {
153                     log.info("there is some fatal errors in fishing operation: " + nuitonValidatorResult.getFieldsForFatal());
154                 }
155                 break;
156             }
157         }
158 //        } else {
159 //            if (log.isInfoEnabled()) {
160 //                log.info("Cruise is not dirty");
161 //            }
162 //        }
163 
164         if (result) {
165 
166             result = Cruises.isDirty(cruise);
167 
168         }
169 
170         if (log.isInfoEnabled()) {
171             log.info("New readyToSynch value: " + result);
172         }
173         setReadyToSynch(result);
174     }
175 
176     public boolean isReadyToSynch() {
177         return readyToSynch;
178     }
179 
180     public void setReadyToSynch(boolean readyToSynch) {
181         this.readyToSynch = readyToSynch;
182         firePropertyChange(PROPERTY_READY_TO_SYNCH, null /*force to fire the changes*/, readyToSynch);
183     }
184 
185     public void setCruise(Cruise cruise) {
186         this.cruise = cruise;
187         computeReadyToSynch();
188     }
189 
190     public Cruise getCruise() {
191         return cruise;
192     }
193 }