View Javadoc
1   package fr.ifremer.tutti.service.genericformat.importactions;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.collect.Sets;
28  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
29  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
30  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
31  import fr.ifremer.tutti.persistence.entities.referential.Gear;
32  import fr.ifremer.tutti.persistence.entities.referential.Person;
33  import fr.ifremer.tutti.persistence.entities.referential.Species;
34  import fr.ifremer.tutti.persistence.entities.referential.Speciess;
35  import fr.ifremer.tutti.persistence.entities.referential.Vessel;
36  import fr.ifremer.tutti.service.PersistenceService;
37  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
38  import fr.ifremer.tutti.service.genericformat.GenericFormatImportRequest;
39  import fr.ifremer.tutti.service.genericformat.GenericFormatReferentialImportResult;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import java.util.HashSet;
44  import java.util.Set;
45  import java.util.stream.Collectors;
46  
47  /**
48   * Created on 3/25/15.
49   *
50   * @author Tony Chemit - chemit@codelutin.com
51   * @since 3.15
52   */
53  public class RestoreAfterValidateAction extends ImportActionSupport {
54  
55      /** Logger. */
56      private static final Log log = LogFactory.getLog(RestoreAfterValidateAction.class);
57  
58      private final PersistenceService persistenceService;
59  
60      public RestoreAfterValidateAction(GenericFormatContextSupport importContext, PersistenceService persistenceService) {
61          super(importContext);
62          this.persistenceService = persistenceService;
63      }
64  
65      @Override
66      protected boolean canExecute() {
67          return true;
68      }
69  
70      @Override
71      protected void doExecute() {
72  
73          Set<Runnable> actions = new HashSet<>();
74          actions.add(() -> rollbackSampleCategoryModel(importContext.getImportRequest()));
75          actions.add(this::rollbackProtocol);
76          actions.add(() -> {
77              // push back if any previous protocol
78              rollbackPreviousProtocol(importContext.getImportRequest());
79          });
80          actions.add(this::rollbackTemporaryGears);
81          actions.add(this::rollbackTemporaryPersons);
82          actions.add(this::rollbackTemporarySpecies);
83          actions.add(this::rollbackTemporaryVessels);
84  
85          for (Runnable action : actions) {
86              try {
87                  action.run();
88              } catch (Exception e) {
89                  if (log.isErrorEnabled()) {
90                      log.error("Could not execute rollback action", e);
91                  }
92              }
93          }
94  
95      }
96  
97      protected void rollbackSampleCategoryModel(GenericFormatImportRequest importRequest) {
98  
99          SampleCategoryModel sampleCategoryModel = importRequest.getSampleCategoryModel();
100 
101         if (log.isInfoEnabled()) {
102             log.info("Rollback previous sample cateogry model: " + sampleCategoryModel);
103         }
104         persistenceService.setSampleCategoryModel(sampleCategoryModel);
105 
106     }
107 
108     protected void rollbackPreviousProtocol(GenericFormatImportRequest importRequest) {
109 
110         TuttiProtocol previousProtocol = importRequest.getProtocol();
111         if (previousProtocol != null) {
112 
113             if (log.isInfoEnabled()) {
114                 log.info("Rollback previous protocol: " + previousProtocol);
115             }
116             persistenceService.setProtocol(previousProtocol);
117 
118         }
119 
120     }
121 
122     protected void rollbackProtocol() {
123 
124         TuttiProtocol importedProtocol = importContext.getImportedProtocol();
125         if (importedProtocol != null) {
126 
127             if (log.isInfoEnabled()) {
128                 log.info("Delete imported protocol: " + importedProtocol);
129             }
130             persistenceService.setProtocol(null);
131             persistenceService.deleteProtocol(importedProtocol.getId());
132 
133         }
134 
135     }
136 
137     protected void rollbackTemporaryGears() {
138 
139         GenericFormatReferentialImportResult<Gear, Integer> fileResult = importContext.getReferentialTemporaryGearFileResult();
140         Set<Integer> addedEntriesIds = Sets.newHashSet(fileResult.getEntitiesAdded().stream().map(TuttiEntities.GET_ID_AS_INT::apply).collect(Collectors.toList()));
141         if (!addedEntriesIds.isEmpty()) {
142             if (log.isInfoEnabled()) {
143                 log.info("Rollback previous imported temporary gears: " + addedEntriesIds);
144             }
145             persistenceService.deleteTemporaryGears(addedEntriesIds);
146         }
147 
148     }
149 
150     protected void rollbackTemporaryPersons() {
151 
152         GenericFormatReferentialImportResult<Person, Integer> fileResult = importContext.getReferentialTemporaryPersonFileResult();
153         Set<Integer> addedEntriesIds = Sets.newHashSet(fileResult.getEntitiesAdded().stream().map(TuttiEntities.GET_ID_AS_INT::apply).collect(Collectors.toList()));
154         if (!addedEntriesIds.isEmpty()) {
155             if (log.isInfoEnabled()) {
156                 log.info("Rollback previous imported temporary persons: " + addedEntriesIds);
157             }
158             persistenceService.deleteTemporaryPersons(addedEntriesIds);
159         }
160 
161     }
162 
163     protected void rollbackTemporarySpecies() {
164 
165         GenericFormatReferentialImportResult<Species, Integer> fileResult = importContext.getReferentialTemporarySpeciesFileResult();
166         Set<Integer> addedEntriesIds = Sets.newHashSet(fileResult.getEntitiesAdded().stream().map(Speciess.GET_REFERECE_TAXON_ID_AS_INT::apply).collect(Collectors.toList()));
167         if (!addedEntriesIds.isEmpty()) {
168             if (log.isInfoEnabled()) {
169                 log.info("Rollback previous imported temporary species: " + addedEntriesIds);
170             }
171             persistenceService.deleteTemporarySpecies(addedEntriesIds);
172         }
173 
174     }
175 
176     protected void rollbackTemporaryVessels() {
177 
178         GenericFormatReferentialImportResult<Vessel, String> fileResult = importContext.getReferentialTemporaryVesselFileResult();
179         Set<String> addedEntriesIds = Sets.newHashSet(fileResult.getEntitiesAdded().stream().map(TuttiEntities.GET_ID::apply).collect(Collectors.toList()));
180         if (!addedEntriesIds.isEmpty()) {
181             if (log.isInfoEnabled()) {
182                 log.info("Rollback previous imported temporary vessels: " + addedEntriesIds);
183             }
184             persistenceService.deleteTemporaryVessels(addedEntriesIds);
185         }
186 
187     }
188 
189 }