1 package org.argeo.people.core.imports;
2
3 import static org.argeo.connect.util.ConnectUtils.notEmpty;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URI;
8 import java.util.Map;
9 import java.util.UUID;
10
11 import javax.jcr.Node;
12 import javax.jcr.Property;
13 import javax.jcr.RepositoryException;
14 import javax.jcr.Session;
15
16 import org.apache.commons.io.IOUtils;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.argeo.connect.ConnectNames;
20 import org.argeo.connect.resources.ResourcesNames;
21 import org.argeo.connect.resources.ResourcesService;
22 import org.argeo.connect.util.ConnectJcrUtils;
23 import org.argeo.jcr.JcrUtils;
24 import org.argeo.people.ContactValueCatalogs;
25 import org.argeo.people.PeopleException;
26 import org.argeo.people.PeopleService;
27 import org.argeo.people.PeopleTypes;
28 import org.argeo.people.util.OrgJcrUtils;
29 import org.argeo.people.util.PeopleJcrUtils;
30 import org.argeo.people.util.PersonJcrUtils;
31
32
33 public class PersonCsvFileParser extends AbstractPeopleCsvFileParser {
34 private final static Log log = LogFactory.getLog(PersonCsvFileParser.class);
35
36 private final String[] personProps = { PEOPLE_PRIMARY_EMAIL, PEOPLE_SALUTATION, PEOPLE_HONORIFIC_TITLE,
37 PEOPLE_NAME_SUFFIX, PEOPLE_NICKNAME, PEOPLE_MAIDEN_NAME, PEOPLE_NICKNAME };
38
39 private Node peopleParentNode;
40
41 public PersonCsvFileParser(Session adminSession, ResourcesService resourcesService, PeopleService peopleService,
42 URI images) {
43 super(adminSession, resourcesService, peopleService, images);
44 peopleParentNode = ConnectJcrUtils.getNode(adminSession, null,
45 peopleService.getBaseRelPath(PeopleTypes.PEOPLE_PERSON));
46 }
47
48 public PersonCsvFileParser(Session adminSession, ResourcesService resourcesService, PeopleService peopleService) {
49 this(adminSession, resourcesService, peopleService, null);
50 }
51
52 @Override
53 protected void processLine(Integer lineNumber, Map<String, String> line) {
54 try {
55
56 String lastName = line.get(PEOPLE_LAST_NAME).trim();
57 String firstName = line.get(PEOPLE_FIRST_NAME).trim();
58 String connectUid = UUID.randomUUID().toString();
59
60
61 String relPath = getPeopleService().getDefaultRelPath(getSession(), PeopleTypes.PEOPLE_PERSON, connectUid);
62 Node person = JcrUtils.mkdirs(peopleParentNode, relPath);
63 person.addMixin(PeopleTypes.PEOPLE_PERSON);
64
65
66 person.setProperty(ConnectNames.CONNECT_UID, connectUid);
67 if (notEmpty(lastName))
68 person.setProperty(PEOPLE_LAST_NAME, lastName);
69 if (notEmpty(firstName))
70 person.setProperty(PEOPLE_FIRST_NAME, firstName);
71 person.setProperty(Property.JCR_TITLE, getPeopleService().getDisplayName(person));
72
73
74 InputStream is = null;
75 try {
76 String imgName = lastName.trim().toLowerCase() + ".jpg";
77 URI myImg = getPicture(imgName);
78
79 is = myImg.toURL().openStream();
80 PeopleJcrUtils.setEntityPicture(person, is, imgName);
81
82
83
84
85
86
87
88
89
90 } catch (IOException ioe) {
91
92 } finally {
93 IOUtils.closeQuietly(is);
94 }
95
96
97 for (String propName : personProps) {
98 String value = line.get(propName);
99 if (value != null && !value.trim().equals(""))
100 person.setProperty(propName, value);
101 }
102
103
104 String tags = line.get(ResourcesNames.CONNECT_TAGS);
105 if (notEmpty(tags))
106 person.setProperty(ResourcesNames.CONNECT_TAGS, ConnectJcrUtils.parseAndClean(tags, ",", true));
107
108
109 String mailingLists = line.get(PEOPLE_MAILING_LISTS);
110 if (notEmpty(mailingLists))
111 person.setProperty(PEOPLE_MAILING_LISTS, ConnectJcrUtils.parseAndClean(mailingLists, ",", true));
112
113
114
115
116 String phone = line.get("people:phoneNumber").trim();
117 if (notEmpty(phone)) {
118 PeopleJcrUtils.createContact(getResourcesService(), getPeopleService(), person,
119 PeopleTypes.PEOPLE_MOBILE, phone, true, ContactValueCatalogs.CONTACT_NATURE_PRO, null);
120 }
121
122 phone = line.get("PhoneDirect").trim();
123 if (notEmpty(phone)) {
124 PeopleJcrUtils.createPhone(getResourcesService(), getPeopleService(), person, phone, false,
125 ContactValueCatalogs.CONTACT_CAT_MAIN, null);
126 }
127
128 String emailAddress = JcrUtils.replaceInvalidChars(line.get("people:emailAddress").trim());
129 if (notEmpty(emailAddress)) {
130 PeopleJcrUtils.createEmail(getResourcesService(), getPeopleService(), person, emailAddress, true, null,
131 null);
132 }
133
134 emailAddress = JcrUtils.replaceInvalidChars(line.get("people:emailAddressOther").trim());
135 if (notEmpty(emailAddress)) {
136 PeopleJcrUtils.createEmail(getResourcesService(), getPeopleService(), person, emailAddress, false, null,
137 null);
138 }
139
140 String facebook = line.get("Facebook");
141 if (notEmpty(facebook)) {
142 PeopleJcrUtils.createSocialMedia(getResourcesService(), getPeopleService(), person, facebook, true,
143 ContactValueCatalogs.CONTACT_CAT_FACEBOOK, null);
144 }
145
146
147 String birthDate = line.get(PEOPLE_BIRTH_DATE);
148 if (notEmpty(birthDate))
149 setDateValueFromString(person, PEOPLE_BIRTH_DATE, birthDate);
150
151
152 String note = line.get(Property.JCR_DESCRIPTION);
153 if (notEmpty(note))
154 person.setProperty(Property.JCR_DESCRIPTION, note);
155
156
157 String orgWebsite = line.get(PeopleTypes.PEOPLE_ORG);
158 if (notEmpty(orgWebsite)) {
159 Node orga = OrgJcrUtils.getOrgWithWebSite(getSession(), orgWebsite);
160 if (orga != null) {
161 PersonJcrUtils.addJob(getResourcesService(), getPeopleService(), person, orga,
162 line.get(PEOPLE_ROLE), false);
163 }
164 }
165
166 getPeopleService().saveEntity(person, true);
167 if (log.isDebugEnabled())
168 log.debug("Test data: loaded " + lastName);
169 } catch (RepositoryException e) {
170 throw new PeopleException("Cannot process line " + lineNumber + " " + line, e);
171 }
172 }
173 }