View Javadoc

1   /*
2    * #%L
3    * Wao :: Business
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2009 - 2011 Ifremer
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Affero General Public License as published by
12   * the Free Software Foundation, either version 3 of the License, or
13   * (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU Affero General Public License
21   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22   * #L%
23   */
24  package fr.ifremer.wao.business.fixtures;
25  
26  import fr.ifremer.wao.WaoBusinessException;
27  import fr.ifremer.wao.WaoDAOHelper;
28  import fr.ifremer.wao.WaoServices;
29  import fr.ifremer.wao.bean.ConnectedUser;
30  import fr.ifremer.wao.bean.ContactState;
31  import fr.ifremer.wao.bean.ObsProgram;
32  import fr.ifremer.wao.bean.UserRole;
33  import fr.ifremer.wao.entity.Boat;
34  import fr.ifremer.wao.entity.BoatDAO;
35  import fr.ifremer.wao.entity.Company;
36  import fr.ifremer.wao.entity.Contact;
37  import fr.ifremer.wao.entity.ContactDAO;
38  import fr.ifremer.wao.entity.ContactImpl;
39  import fr.ifremer.wao.entity.ContactStateMotif;
40  import fr.ifremer.wao.entity.ElligibleBoat;
41  import fr.ifremer.wao.entity.ElligibleBoatDAO;
42  import fr.ifremer.wao.entity.SampleRow;
43  import fr.ifremer.wao.entity.SampleRowDAO;
44  import fr.ifremer.wao.entity.WaoUser;
45  import fr.ifremer.wao.service.ServiceContact;
46  import fr.ifremer.wao.service.ServiceSampling;
47  import org.apache.commons.io.IOUtils;
48  import org.nuiton.topia.TopiaException;
49  import org.nuiton.util.DateUtil;
50  
51  import java.io.InputStream;
52  import java.util.Date;
53  
54  /**
55   * This class provides fixtures for the ObsMer program, data-set described
56   * here has been extracted from copy-pasted code found everywhere in tests.
57   */
58  public class ObsMerFixtures extends Fixtures {
59  
60      public ObsMerFixtures(WaoServices manager) {
61          super(manager);
62      }
63  
64      @Override
65      protected ObsProgram getObsProgram() {
66          return ObsProgram.OBSMER;
67      }
68  
69      /** a company name 'TARTANPION' */
70      public Company tartanpion() {
71          return getCompany("TARTANPION");
72      }
73  
74  
75      /** a company name 'BIS' */
76      public Company bis() {
77          return getCompany("BIS");
78      }
79  
80      protected boolean fishingZonesImported = false;
81  
82      public void fishingZones() {
83          if ( ! fishingZonesImported) {
84              InputStream input = null;
85              try {
86                  input = getClass().getResourceAsStream("/import/zonesPeche.csv");
87                  manager.getServiceReferential().importFishingZoneCsv(input);
88              } finally {
89                  IOUtils.closeQuietly(input);
90              }
91              fishingZonesImported = true;
92          }
93      }
94  
95      public WaoUser jeanMichmuche() {
96          return getWaoUser("jmichmuche", "mdp", "Jean", "Michmuche", tartanpion());
97      }
98  
99      public WaoUser jacquesMichmuche() {
100         return getWaoUser("j.michmuche", "mdp", "Jacques", "Michmuche", tartanpion());
101     }
102 
103     public WaoUser billMurray() {
104         return getWaoUser("BillMurray", "mdp", "Bill", "Murray", bis());
105     }
106 
107     public ConnectedUser jeanMichmucheAsAdmin() {
108         ConnectedUser connectedUser = connectedUser(jeanMichmuche(), UserRole.ADMIN);
109         return connectedUser;
110     }
111 
112     @Override
113     protected void importSamplingPlan() {
114         tartanpion();
115         bis();
116         fishingZones();
117         ServiceSampling serviceSampling = manager.getServiceSampling();
118         InputStream input = null;
119         try {
120             input = getClass().getResourceAsStream("/import/echantillonnage.csv");
121             serviceSampling.importSamplingPlanCsv(input, jeanMichmucheAsAdmin());
122         } catch (WaoBusinessException e) {
123             throw new RuntimeException(e);
124         } finally {
125             IOUtils.closeQuietly(input);
126         }
127     }
128 
129     public ConnectedUser jeanMichmucheAsCoordinator() {
130         return connectedUser(jeanMichmuche(), UserRole.COORDINATOR);
131     }
132 
133     public ConnectedUser jeanMichmucheAsObserver() {
134         return connectedUser(jeanMichmuche(), UserRole.OBSERVER);
135     }
136 
137     public ConnectedUser billMurrayAsObserver() {
138         return connectedUser(billMurray(), UserRole.OBSERVER);
139     }
140 
141     /**
142      * @return a boat '177474'
143      */
144     public Boat mayflowers() {
145         return getBoat(177474);
146     }
147 
148     public void contacts() {
149         Boat boat = samourai();
150         Boat boat2 = mayflowers();
151         WaoUser user = jeanMichmuche();
152         WaoUser user2 = billMurray();
153         samplingPlan();
154 
155         try {
156             manager.setCurrentDate(DateUtil.createDate(5, 10, 2010));
157 
158             SampleRowDAO rowDAO = WaoDAOHelper.getSampleRowDAO(getTransaction());
159             SampleRow row = rowDAO.findByCode("2010_M0001");
160             SampleRow row2 = rowDAO.findByCode("2009_M0006");
161 
162             ServiceContact serviceContact = manager.getServiceContact();
163 
164             ConnectedUser connectedUser = jeanMichmucheAsAdmin();
165 
166             // Contacts for company TARTANPION
167             Contact contact1 = new ContactImpl();
168             contact1.setBoat(boat);
169             contact1.setMainObserver(user);
170             contact1.setSampleRow(row);
171             contact1.setContactState(ContactState.OBSERVATION_DONE);
172             Date begin = DateUtil.createDate(3, 3, 2010);
173             contact1.setObservationBeginDate(begin);
174             Date end = DateUtil.createDate(4, 3, 2010);
175             contact1.setObservationEndDate(end);
176             contact1.setDataInputDate(end);
177             contact1.setRestitution(end);
178             serviceContact.saveContact(connectedUser, contact1, false);
179 
180             Contact contact2 = new ContactImpl();
181             contact2.setBoat(boat);
182             contact2.setMainObserver(user);
183             contact2.setSampleRow(row);
184             contact2.setContactState(ContactState.OBSERVATION_DONE);
185             begin = DateUtil.createDate(3, 10, 2010);
186             contact2.setObservationBeginDate(begin);
187             end = DateUtil.createDate(4, 10, 2010);
188             contact2.setObservationEndDate(end);
189             contact2.setDataInputDate(end);
190             contact2.setRestitution(end);
191             serviceContact.saveContact(connectedUser, contact2, false);
192 
193             Contact contact3 = new ContactImpl();
194             contact3.setBoat(boat);
195             contact3.setMainObserver(user);
196             contact3.setSampleRow(row);
197             contact3.setContactState(ContactState.CONTACT_REFUSED);
198             contact3.setContactStateMotif(defaultContactStateMotif());
199             begin = DateUtil.createDate(3, 5, 2010);
200             end = DateUtil.createDate(4, 5, 2010);
201             contact3.setObservationEndDate(end);
202             contact3.setCreationDate(begin);
203             serviceContact.saveContact(connectedUser, contact3, false);
204 
205             // Contacts for company BIS
206             Contact contact4 = new ContactImpl();
207             contact4.setBoat(boat2);
208             contact4.setMainObserver(user2);
209             contact4.setSampleRow(row2);
210             contact4.setContactState(ContactState.OBSERVATION_EXPECTED);
211             begin = DateUtil.createDate(3, 3, 2010);
212             end = DateUtil.createDate(4, 3, 2010);
213             contact4.setObservationEndDate(end);
214             contact4.setCreationDate(begin);
215             serviceContact.saveContact(connectedUser, contact4, false);
216 
217             Contact contact5 = new ContactImpl();
218             contact5.setBoat(boat2);
219             contact5.setMainObserver(user2);
220             contact5.setSampleRow(row2);
221             contact5.setContactState(ContactState.OBSERVATION_CANCELLED);
222             contact5.setComment("commentaire obligatoire");
223             begin = DateUtil.createDate(3, 5, 2010);
224             end = DateUtil.createDate(4, 5, 2010);
225             contact5.setObservationEndDate(end);
226             contact5.setCreationDate(begin);
227             serviceContact.saveContact(connectedUser, contact5, false);
228 
229             // Contact refused by program, will not be in result
230             Contact contact6 = new ContactImpl();
231             contact6.setBoat(boat2);
232             contact6.setMainObserver(user2);
233             contact6.setSampleRow(row2);
234             contact6.setContactState(ContactState.OBSERVATION_CANCELLED);
235             contact6.setComment("commentaire obligatoire");
236             begin = DateUtil.createDate(3, 3, 2010);
237             contact6.setCreationDate(begin);
238             end = DateUtil.createDate(4, 3, 2010);
239             contact6.setObservationEndDate(end);
240             contact6.setValidationProgram(Boolean.FALSE);
241             serviceContact.saveContact(connectedUser, contact6, false);
242         } catch (TopiaException e) {
243             throw new RuntimeException(e);
244         }
245     }
246 
247     /** @return a boat '174592' */
248     public Boat fifitou() {
249         return getBoat(174592);
250     }
251 
252     public SampleRow row2009_0003() {
253         return getSampleRow("2009_M0003");
254     }
255 
256     public SampleRow row2010_0004() {
257         return getSampleRow("2010_M0004");
258     }
259 
260     public SampleRow row2010_0001() {
261         return getSampleRow("2010_M0001");
262     }
263 
264     public SampleRow row2009_0006() {
265         return getSampleRow("2009_M0006");
266     }
267 
268     protected Boat springfield;
269 
270     public Boat springfield() {
271         if (springfield == null) {
272             try {
273                 BoatDAO dao = WaoDAOHelper.getBoatDAO(getTransaction());
274                 springfield = dao.create();
275                 springfield.setImmatriculation(123456);
276                 springfield.setName("SPRINGFIELD");
277                 springfield.setActive(true);
278                 transaction.commitTransaction();
279             } catch (TopiaException e) {
280                 throw new RuntimeException(e);
281             }
282         }
283         return springfield;
284     }
285 
286     public WaoUser homerSimpson() {
287         return getWaoUser("homer", "doh", "Homer", "Simpson", tartanpion());
288     }
289 
290     public ConnectedUser homerSimpsonAsObserver() {
291         return connectedUser(homerSimpson(), UserRole.OBSERVER);
292     }
293 
294     protected SampleRow defaultSampleRow;
295 
296     public SampleRow defaultSampleRow() {
297         if (defaultSampleRow == null) {
298             try {
299                 SampleRowDAO dao = WaoDAOHelper.getSampleRowDAO(getTransaction());
300                 defaultSampleRow = dao.create();
301                 defaultSampleRow.setCode("2010_0001");
302                 defaultSampleRow.setCompany(tartanpion());
303 
304                 ElligibleBoatDAO elligibleBoatDAO = WaoDAOHelper.getElligibleBoatDAO(getTransaction());
305                 ElligibleBoat elligibleBoat = elligibleBoatDAO.create();
306                 elligibleBoat.setSampleRow(defaultSampleRow());
307                 elligibleBoat.setBoat(samourai());
308                 elligibleBoat.setCompanyActive(true);
309                 elligibleBoat.setGlobalActive(true);
310 
311                 defaultSampleRow.addElligibleBoat(elligibleBoat);
312                 getTransaction().commitTransaction();
313             } catch (TopiaException e) {
314                 throw new RuntimeException(e);
315             }
316         }
317         return defaultSampleRow;
318     }
319 
320     protected Contact defaultContact;
321 
322     public Contact defaultContact() {
323         if (defaultContact == null) {
324             try {
325                 ContactDAO dao = WaoDAOHelper.getContactDAO(getTransaction());
326                 defaultContact = dao.create();
327                 defaultContact.setBoat(springfield());
328                 defaultContact.setContactState(ContactState.CONTACT_START);
329                 defaultContact.setMainObserver(homerSimpson());
330                 defaultContact.setSampleRow(defaultSampleRow());
331                 getTransaction().commitTransaction();
332             } catch (TopiaException e) {
333                 throw new RuntimeException(e);
334             }
335         }
336         return defaultContact;
337     }
338 
339     protected ContactStateMotif defaultContactStateMotif;
340 
341     public ContactStateMotif defaultContactStateMotif() {
342         if (defaultContactStateMotif == null) {
343             defaultContactStateMotif = manager.getServiceReferential().
344                     getAllContactStateMotifs(ContactState.CONTACT_REFUSED).
345                     get(2);
346         }
347         return defaultContactStateMotif;
348     }
349 }