View Javadoc
1   package org.argeo.people.core.imports;
2   
3   import java.util.Map;
4   
5   import javax.jcr.Node;
6   import javax.jcr.RepositoryException;
7   
8   import org.argeo.connect.util.ConnectJcrUtils;
9   import org.argeo.people.PeopleConstants;
10  import org.argeo.people.PeopleException;
11  import org.argeo.util.CsvParserWithLinesAsMap;
12  
13  /**
14   * Base utility to load a catalogue of property values for a given business type
15   * in a repository.
16   * 
17   * Expected file format is a .CSV with 2 columns, the first with the property
18   * names and the second with a '; ' separated list of String values.
19   * 
20   * By default, found values are stored in a multi-String property with this
21   * name.
22   * 
23   * If the property name has been prefixed by "single:", found value is stored as
24   * a regular STRING Property
25   **/
26  public class TemplateCatalogueCsvFileParser extends CsvParserWithLinesAsMap {
27  
28  	private final Node node;
29  	private final static String SINGLE_VALUE_PROP_PREFIX = "single:";
30  
31  	public TemplateCatalogueCsvFileParser(Node node) {
32  		super();
33  		this.node = node;
34  	}
35  
36  	@Override
37  	protected void processLine(Integer lineNumber, Map<String, String> line) {
38  		try {
39  			String propName = line
40  					.get(PeopleConstants.IMPORT_CATALOGUE_KEY_COL);
41  			String valuesStr = line
42  					.get(PeopleConstants.IMPORT_CATALOGUE_VALUES_COL);
43  
44  			if (propName.startsWith(SINGLE_VALUE_PROP_PREFIX)) {
45  				node.setProperty(
46  						propName.substring(SINGLE_VALUE_PROP_PREFIX.length()),
47  						valuesStr);
48  			} else {
49  				String[] values = ConnectJcrUtils
50  						.parseAndClean(
51  								valuesStr,
52  								PeopleConstants.IMPORT_CATALOGUE_VALUES_SEPARATOR,
53  								true);
54  				node.setProperty(propName, values);
55  			}
56  		} catch (RepositoryException e) {
57  			throw new PeopleException("Cannot process line " + lineNumber + " "
58  					+ line, e);
59  		}
60  	}
61  }