1 package org.argeo.people.core.imports;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.text.DateFormat;
7 import java.text.ParseException;
8 import java.text.SimpleDateFormat;
9 import java.util.Calendar;
10 import java.util.GregorianCalendar;
11 import java.util.Map;
12
13 import javax.jcr.Node;
14 import javax.jcr.RepositoryException;
15 import javax.jcr.Session;
16 import javax.jcr.version.VersionManager;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.argeo.connect.ConnectException;
21 import org.argeo.connect.resources.ResourcesService;
22 import org.argeo.jcr.JcrUtils;
23 import org.argeo.people.PeopleException;
24 import org.argeo.people.PeopleNames;
25 import org.argeo.people.PeopleService;
26 import org.argeo.util.CsvParserWithLinesAsMap;
27
28
29 public abstract class AbstractPeopleCsvFileParser extends CsvParserWithLinesAsMap implements PeopleNames {
30 private final static Log log = LogFactory.getLog(AbstractPeopleCsvFileParser.class);
31
32 private final Session adminSession;
33 private final ResourcesService resourcesService;
34 private final PeopleService peopleService;
35
36 protected VersionManager vm;
37 protected DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
38
39
40
41 private URI images = null;
42
43
44
45
46
47
48
49
50 public AbstractPeopleCsvFileParser(Session adminSession, ResourcesService resourcesService,
51 PeopleService peopleService, URI images) {
52 super();
53 this.adminSession = adminSession;
54 this.resourcesService = resourcesService;
55 this.peopleService = peopleService;
56 this.images = images;
57 try {
58 vm = adminSession.getWorkspace().getVersionManager();
59 } catch (RepositoryException e) {
60 throw new PeopleException("Unable to get version manager " + "while trying to import nodes", e);
61 }
62 }
63
64 public AbstractPeopleCsvFileParser(Session adminSession, ResourcesService resourcesService,
65 PeopleService peopleService) {
66 this(adminSession, resourcesService, peopleService, null);
67 }
68
69 @Override
70 protected abstract void processLine(Integer lineNumber, Map<String, String> line);
71
72
73 protected void setDateValueFromString(Node node, String propName, String value) {
74 try {
75 Calendar cal = new GregorianCalendar();
76 cal.setTime(dateFormat.parse(value));
77 node.setProperty(propName, cal);
78 } catch (ParseException e) {
79 throw new PeopleException("Unable to parse date for: " + value, e);
80 } catch (RepositoryException e) {
81 throw new PeopleException("Unable to get version manager " + "while trying to import nodes", e);
82 }
83
84 }
85
86 protected URI getPicture(String fileName) throws IOException {
87 String tmpPath = images.getPath();
88 tmpPath = tmpPath.substring(0, tmpPath.length() - 1);
89 tmpPath = JcrUtils.lastPathElement(tmpPath);
90 tmpPath = tmpPath + "/" + fileName;
91 if (log.isTraceEnabled())
92 log.trace("Getting dummy image at path: " + tmpPath);
93
94 try {
95 return new URI(images.getScheme(), images.getSchemeSpecificPart(), tmpPath);
96 } catch (URISyntaxException e) {
97 throw new ConnectException("Badly formatted URI", e);
98 }
99 }
100
101
102 protected Session getSession() {
103 return adminSession;
104 }
105
106 protected ResourcesService getResourcesService() {
107 return resourcesService;
108 }
109
110 protected PeopleService getPeopleService() {
111 return peopleService;
112 }
113 }