View Javadoc
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.PeopleJcrUtils;
29  
30  /**
31   * Base utility to load organisations data in a People repository from a .CSV
32   * file
33   **/
34  public class OrgCsvFileParser extends AbstractPeopleCsvFileParser {
35  	private final static Log log = LogFactory.getLog(OrgCsvFileParser.class);
36  
37  	private Node peopleParentNode;
38  
39  	public OrgCsvFileParser(Session adminSession, ResourcesService resourcesService, PeopleService peopleService,
40  			URI images) {
41  		super(adminSession, resourcesService, peopleService, images);
42  		peopleParentNode = ConnectJcrUtils.getNode(adminSession, null,
43  				peopleService.getBaseRelPath(PeopleTypes.PEOPLE_ORG));
44  	}
45  
46  	public OrgCsvFileParser(Session adminSession, PeopleService peopleService, ResourcesService resourceService) {
47  		this(adminSession, resourceService, peopleService, null);
48  	}
49  
50  	@Override
51  	protected void processLine(Integer lineNumber, Map<String, String> line) {
52  		try {
53  
54  			String connectUid = UUID.randomUUID().toString();
55  			String relPath = getPeopleService().getDefaultRelPath(getSession(), PeopleTypes.PEOPLE_ORG, connectUid);
56  			Node orga = JcrUtils.mkdirs(peopleParentNode, relPath);
57  			orga.addMixin(PeopleTypes.PEOPLE_ORG);
58  
59  			String legalName = line.get(PEOPLE_LEGAL_NAME).trim();
60  			orga.setProperty(PEOPLE_LEGAL_NAME, legalName);
61  			orga.setProperty(Property.JCR_TITLE, legalName);
62  			orga.setProperty(ConnectNames.CONNECT_UID, UUID.randomUUID().toString());
63  
64  			// Website and dummy picture
65  			String webSite = line.get("people:websiteUrl");
66  			if (notEmpty(webSite)) {
67  				PeopleJcrUtils.createWebsite(getResourcesService(), getPeopleService(), orga, webSite, true, null);
68  
69  				// picture
70  				InputStream is = null;
71  				String image = webSite.trim().toLowerCase() + ".jpg";
72  				try {
73  					URI myImg = getPicture(image);
74  //					if (myImg.exists()) {
75  						is = myImg.toURL().openStream();
76  						PeopleJcrUtils.setEntityPicture(orga, is, image);
77  //					}
78  				} catch (IOException ioe) {// Silent
79  					log.warn("Cannot import org image " + image, ioe);
80  				} finally {
81  					IOUtils.closeQuietly(is);
82  				}
83  			}
84  
85  			// address
86  			String street = line.get(PEOPLE_STREET), street2 = line.get(PEOPLE_STREET_COMPLEMENT),
87  					zip = line.get(PEOPLE_ZIP_CODE), city = line.get(PEOPLE_CITY), state = line.get(PEOPLE_STATE),
88  					country = line.get(PEOPLE_COUNTRY);
89  
90  			if (notEmpty(street) || notEmpty(street2) || notEmpty(zip) || notEmpty(city) || notEmpty(state)
91  					|| notEmpty(country))
92  				PeopleJcrUtils.createAddress(getResourcesService(), getPeopleService(), orga, street, street2, zip,
93  						city, state, country, true, ContactValueCatalogs.CONTACT_CAT_HEADOFFICE, null);
94  
95  			String emailAddress = line.get("people:emailAddress").trim();
96  			if (notEmpty(emailAddress)) {
97  				PeopleJcrUtils.createEmail(getResourcesService(), getPeopleService(), orga, emailAddress, true, null,
98  						null);
99  			}
100 
101 			// Phone numbers
102 			String phone = line.get("people:phoneNb");
103 			if (notEmpty(phone))
104 				PeopleJcrUtils.createPhone(getResourcesService(), getPeopleService(), orga, phone, true,
105 						ContactValueCatalogs.CONTACT_CAT_MAIN, null);
106 			phone = line.get("people:faxNb");
107 			if (notEmpty(phone))
108 				PeopleJcrUtils.createContact(getResourcesService(), getPeopleService(), orga, PeopleTypes.PEOPLE_FAX,
109 						phone, true, null, null);
110 
111 			// Tags
112 			String tags = line.get(ResourcesNames.CONNECT_TAGS);
113 			if (notEmpty(tags))
114 				orga.setProperty(ResourcesNames.CONNECT_TAGS, ConnectJcrUtils.parseAndClean(tags, ",", true));
115 
116 			// Mailing lists
117 			String mailingLists = line.get(PEOPLE_MAILING_LISTS);
118 			if (notEmpty(mailingLists))
119 				orga.setProperty(PEOPLE_MAILING_LISTS, ConnectJcrUtils.parseAndClean(mailingLists, ",", true));
120 			getPeopleService().saveEntity(orga, true);
121 
122 			if (log.isDebugEnabled())
123 				log.debug("Test data: loaded " + legalName);
124 
125 		} catch (RepositoryException e) {
126 			throw new PeopleException("Cannot process line " + lineNumber + " " + line, e);
127 		}
128 	}
129 }