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.ArrayListMultimap;
28  import com.google.common.collect.Multimap;
29  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
30  import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
31  import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForAttachment;
32  import fr.ifremer.tutti.service.genericformat.csv.AttachmentRow;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.csv.ImportRow;
36  import org.nuiton.csv.ImportRuntimeException;
37  import org.nuiton.jaxx.application.ApplicationTechnicalException;
38  
39  import java.io.IOException;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * Created on 3/26/15.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 3.14.3
48   */
49  public class LoadAttachmentsAction extends ImportActionSupport {
50  
51      /** Logger. */
52      private static final Log log = LogFactory.getLog(LoadAttachmentsAction.class);
53  
54      public LoadAttachmentsAction(GenericFormatContextSupport importContext) {
55          super(importContext);
56      }
57  
58      @Override
59      protected boolean canExecute() {
60          return importContext.getImportRequest().isImportAttachments();
61      }
62  
63      @Override
64      protected void skipExecute() {
65  
66          importContext.increments(t("tutti.service.genericFormat.skip.load.attachments"));
67  
68          if (!importContext.getImportRequest().isImportAttachments()) {
69              GenericFormatCsvFileResult importFileResult = importContext.getAttachmentFileResult();
70              importFileResult.setSkipped(true);
71          }
72  
73      }
74  
75      @Override
76      protected void doExecute() {
77  
78          if (log.isInfoEnabled()) {
79              log.info("Load attachments definitions.");
80          }
81  
82          importContext.increments(t("tutti.service.genericFormat.load.attachments"));
83  
84          GenericFormatCsvFileResult importFileResult = importContext.getAttachmentFileResult();
85          try (CsvConsumerForAttachment consumer = importContext.loadAttachments(false)) {
86  
87              Multimap<String, AttachmentRow> attachmentRowsByObjectId = ArrayListMultimap.create();
88  
89              for (ImportRow<AttachmentRow> row : consumer) {
90  
91                  consumer.validateRow(row);
92  
93                  if (row.isValid()) {
94  
95                      AttachmentRow bean = row.getBean();
96                      attachmentRowsByObjectId.put(bean.getObjectType() + "_" + bean.getObjectId(), bean);
97  
98                  }
99  
100             }
101 
102             importFileResult.flushErrors(consumer);
103 
104             importContext.setAttachmentRows(attachmentRowsByObjectId);
105 
106         } catch (IOException e) {
107             throw new ApplicationTechnicalException("Could not close attachments.csv file", e);
108         } catch (ImportRuntimeException e) {
109 
110             importFileResult.addGlobalError(e.getMessage());
111 
112         }
113 
114     }
115 
116 }