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 fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
28  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
29  import fr.ifremer.tutti.service.PersistenceService;
30  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatImportRequest;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import java.util.HashSet;
36  import java.util.Set;
37  
38  /**
39   * Created on 3/25/15.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 3.15
43   */
44  public class RestoreAfterImportAction extends ImportActionSupport {
45  
46      /** Logger. */
47      private static final Log log = LogFactory.getLog(RestoreAfterImportAction.class);
48  
49      private final PersistenceService persistenceService;
50  
51      public RestoreAfterImportAction(GenericFormatContextSupport importContext, PersistenceService persistenceService) {
52          super(importContext);
53          this.persistenceService = persistenceService;
54      }
55  
56      @Override
57      protected boolean canExecute() {
58          return true;
59      }
60  
61      @Override
62      protected void doExecute() {
63          
64          Set<Runnable> actions = new HashSet<>();
65          actions.add(() -> rollbackSampleCategoryModel(importContext.getImportRequest()));
66          actions.add(() -> rollbackProtocol(importContext.getImportRequest()));
67  
68          for (Runnable action : actions) {
69              try {
70                  action.run();
71              } catch (Exception e) {
72                  if (log.isErrorEnabled()) {
73                      log.error("Could not execute rollback action", e);
74                  }
75              }
76          }
77  
78      }
79  
80      protected void rollbackSampleCategoryModel(GenericFormatImportRequest importRequest) {
81  
82          SampleCategoryModel sampleCategoryModel = importRequest.getSampleCategoryModel();
83  
84          if (log.isInfoEnabled()) {
85              log.info("Rollback previous sample cateogry model: " + sampleCategoryModel);
86          }
87          persistenceService.setSampleCategoryModel(sampleCategoryModel);
88  
89      }
90  
91      protected void rollbackProtocol(GenericFormatImportRequest importRequest) {
92  
93          if (importRequest.isOverrideProtocol()) {
94  
95              String protocolOriginalName = importContext.getProtocolOriginalName();
96  
97              TuttiProtocol toDelete = persistenceService.getProtocolByName(protocolOriginalName);
98  
99              if (log.isInfoEnabled()) {
100                 log.info("Delete previous protocol: " + toDelete.getId() + " - " + toDelete.getName());
101             }
102 
103             // delete previous protocol
104             persistenceService.deleteProtocol(toDelete.getId());
105 
106             TuttiProtocol importedProtocol = importContext.getImportedProtocol();
107             importedProtocol.setName(protocolOriginalName);
108             if (log.isInfoEnabled()) {
109                 log.info("Save imported protocol: " + importedProtocol.getId() + " - " + importedProtocol.getName());
110             }
111 
112             persistenceService.saveProtocol(importedProtocol);
113 
114         }
115 
116     }
117 
118 }