1 package com.atlassian.dbexporter.importer;
2
3 import com.atlassian.dbexporter.Context;
4 import com.atlassian.dbexporter.ImportExportErrorService;
5 import com.atlassian.dbexporter.node.NodeParser;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.ListIterator;
12
13 import static com.google.common.base.Preconditions.checkArgument;
14 import static com.google.common.base.Preconditions.checkNotNull;
15
16 public abstract class AbstractImporter implements Importer {
17 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
18
19 protected final ImportExportErrorService errorService;
20 private List<AroundImporter> arounds;
21
22 protected AbstractImporter(ImportExportErrorService errorService) {
23 this(errorService, Collections.<AroundImporter>emptyList());
24 }
25
26 protected AbstractImporter(ImportExportErrorService errorService, List<AroundImporter> arounds) {
27 this.errorService = checkNotNull(errorService);
28 this.arounds = checkNotNull(arounds);
29 }
30
31 @Override
32 public final void importNode(NodeParser node, ImportConfiguration configuration, Context context) {
33 checkNotNull(node);
34 checkArgument(!node.isClosed(), "Node must not be closed to be imported! " + node);
35 checkArgument(supports(node), "Importer called on unsupported node! " + node);
36 checkNotNull(context);
37
38 logger.debug("Importing node {}", node);
39
40 for (AroundImporter around : arounds) {
41 around.before(node, configuration, context);
42 }
43
44 doImportNode(node, configuration, context);
45
46 for (ListIterator<AroundImporter> iterator = arounds.listIterator(arounds.size()); iterator.hasPrevious(); ) {
47 iterator.previous().after(node, configuration, context);
48 }
49 }
50
51 protected abstract void doImportNode(NodeParser node, ImportConfiguration configuration, Context context);
52 }