Java Examples for au.com.bytecode.opencsv.bean.HeaderColumnNameTranslateMappingStrategy
The following java examples will help you to understand the usage of au.com.bytecode.opencsv.bean.HeaderColumnNameTranslateMappingStrategy. These source code samples are taken from different open source projects.
Example 1
| Project: seqware-master File: FileLinkerParser.java View source code |
@VisibleForTesting
static List<FileLinkerLine> getFileInfo(Reader reader, char separator) {
CSVReader csvReader = new CSVReader(reader, separator);
HeaderColumnNameTranslateMappingStrategy<FileLinkerLine> strat = new HeaderColumnNameTranslateMappingStrategy<>();
strat.setType(FileLinkerLine.class);
Map<String, String> map = Maps.newHashMap();
map.put("sequencer_run", "sequencerRun");
map.put("sample", "sample");
map.put("lane", "laneString");
map.put("ius_sw_accession", "seqwareAccessionString");
map.put("file_status", "fileStatus");
map.put("mime_type", "mimeType");
map.put("size", "sizeString");
map.put("md5sum", "md5sum");
map.put("file", "filename");
strat.setColumnMapping(map);
CsvToBean<FileLinkerLine> csvToBean = new CsvToBean<>();
List<FileLinkerLine> defaultUsers = csvToBean.parse(strat, csvReader);
return defaultUsers;
}Example 2
| Project: Enterprise-Application-Samples-master File: ContactsLoader.java View source code |
@Override
protected List<AddressObject> doInBackground() {
//sometimes the initial delay can be long,
//so let the user know we're working on it.
//by giving a little progress
CsvToBean<AddressObject> bean = new CsvToBean<AddressObject>();
Map<String, String> columnMapping = new HashMap<String, String>();
//mapping is (CSV Header, Bean element)
columnMapping.put(groupName, "groupName");
columnMapping.put("LastName", "lastName");
columnMapping.put("FirstName", "firstName");
columnMapping.put("email", "email");
columnMapping.put("homephone", "homePhone");
columnMapping.put("workphone", "workPhone");
columnMapping.put("cellphone", "mobilePhone");
columnMapping.put("pin", "pin");
columnMapping.put("address1", "address1");
columnMapping.put("address2", "address2");
columnMapping.put("city", "city");
columnMapping.put("state", "state");
columnMapping.put("zip", "zip");
columnMapping.put("country", "country");
columnMapping.put("Title", "title");
columnMapping.put("Company", "company");
try {
StringBuilder fileContents = new StringBuilder((int) source.length());
Scanner scanner = new Scanner(source);
String lineSeparator = System.getProperty("line.separator");
while (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
csvString = fileContents.toString();
HeaderColumnNameTranslateMappingStrategy<AddressObject> strategy = new HeaderColumnNameTranslateMappingStrategy<AddressObject>();
strategy.setType(AddressObject.class);
strategy.setColumnMapping(columnMapping);
//Parse the CSV
contacts = bean.parse(strategy, new StringReader(csvString));
for (AddressObject ao : contacts) {
if (!this.isCancelled()) {
ao.setGroupName(groupName);
System.out.println("object id = " + ao.getId() + " stuff= " + ao.getAddressListObject());
int id = addressDao.saveRecord(ao);
AddressObject adObj = new AddressObject(ao.getGroupName(), ao.getLastName(), ao.getFirstName(), ao.getEmail(), ao.getHomePhone(), ao.getWorkPhone(), ao.getMobilePhone(), ao.getPin(), ao.getAddress1(), ao.getAddress2(), ao.getCity(), ao.getState(), ao.getZip(), ao.getCountry(), ao.getTitle(), ao.getCompany(), id);
contactsPanel.addContactsListEntry(adObj);
}
}
} catch (IOException ex) {
System.out.println("ContactsLoader IOException: " + ex.getMessage());
} catch (Exception e) {
System.out.println("Major failure in ContactsLoader Thread. " + e.getMessage());
}
return contacts;
}Example 3
| Project: Fudan-Sakai-master File: ProfileConverter.java View source code |
/**
* Import profiles from the given CSV file
*
* <p>The CSV file may contain any of the following headings, in any order:
*
* <ul>
* <li>eid</li>
* <li>nickname</li>
* <li>position</li>
* <li>department</li>
* <li>school</li>
* <li>room</li>
* <li>web site</li>
* <li>work phone</li>
* <li>home phone</li>
* <li>mobile phone</li>
* <li>fax</li>
* <li>books</li>
* <li>tv</li>
* <li>movies</li>
* <li>quotes</li>
* <li>summary</li>
* <li>course</li>
* <li>subjects</li>
* <li>staff profile</li>
* <li>uni profile url</li>
* <li>academic profile url</li>
* <li>publications</li>
* <li>official image url</li>
* </ul>
*
* <p>Column headings must match EXACTLY the list above. They do not need to be in the same order, or even all present.
*
* <p>Fields must be comma separated and each field surrounded with double quotes. There must be no spaces between fields.
*
* <p>Only users that do not currently have a profile will be imported.
*
* @param path path to CSV file on the server
*/
public void importProfiles(String path) {
if (StringUtils.isBlank(path)) {
log.warn("Profile2 importer: invalid path to CSV file. Aborting.");
return;
}
HeaderColumnNameTranslateMappingStrategy<ImportableUserProfile> strat = new HeaderColumnNameTranslateMappingStrategy<ImportableUserProfile>();
strat.setType(ImportableUserProfile.class);
//map the column headers to the field names in the UserProfile class
//this mapping is not exhaustive and can be added to at any time since we are mapping
//on column name not position
Map<String, String> map = new HashMap<String, String>();
map.put("eid", "eid");
map.put("nickname", "nickname");
map.put("position", "position");
map.put("department", "department");
map.put("school", "school");
map.put("room", "room");
map.put("web site", "homepage");
map.put("work phone", "workphone");
map.put("home phone", "homephone");
map.put("mobile phone", "mobilephone");
map.put("fax", "facsimile");
map.put("books", "favouriteBooks");
map.put("tv", "favouriteTvShows");
map.put("movies", "favouriteMovies");
map.put("quotes", "favouriteQuotes");
map.put("summary", "personalSummary");
map.put("course", "course");
map.put("subjects", "subjects");
map.put("staff profile", "staffProfile");
map.put("uni profile url", "universityProfileUrl");
map.put("academic profile url", "academicProfileUrl");
map.put("publications", "publications");
map.put("official image url", "officialImageUrl");
strat.setColumnMapping(map);
CsvToBean<ImportableUserProfile> csv = new CsvToBean<ImportableUserProfile>();
List<ImportableUserProfile> list = new ArrayList<ImportableUserProfile>();
try {
list = csv.parse(strat, new CSVReader(new FileReader(path)));
} catch (FileNotFoundException fnfe) {
log.error("Profile2 importer: Couldn't find file: " + fnfe.getClass() + " : " + fnfe.getMessage());
}
//setup a security advisor so we can save profiles
SecurityAdvisor securityAdvisor = new SecurityAdvisor() {
public SecurityAdvice isAllowed(String userId, String function, String reference) {
return SecurityAdvice.ALLOWED;
}
};
enableSecurityAdvisor(securityAdvisor);
//process each
for (ImportableUserProfile profile : list) {
log.info("Processing user: " + profile.getEid());
//get uuid
String uuid = sakaiProxy.getUserIdForEid(profile.getEid());
if (StringUtils.isBlank(uuid)) {
log.error("Invalid user: " + profile.getEid() + ". Skipping...");
continue;
}
profile.setUserUuid(uuid);
//check if user already has a profile. Skip if so.
if (hasPersistentProfile(uuid)) {
log.warn("User: " + profile.getEid() + " already has a profile. Skipping...");
continue;
}
//persist user profile
try {
SakaiPerson sp = transformUserProfileToSakaiPerson(profile);
if (sp == null) {
//already logged
continue;
}
if (sakaiProxy.updateSakaiPerson(sp)) {
log.info("Profile saved for user: " + profile.getEid());
} else {
log.error("Couldn't save profile for user: " + profile.getEid());
continue;
}
} catch (ProfileNotDefinedException pnde) {
continue;
}
//add/update official image, if supplied in the CSV
if (StringUtils.isNotBlank(profile.getOfficialImageUrl())) {
if (imageLogic.saveOfficialImageUrl(uuid, profile.getOfficialImageUrl())) {
log.info("Official image saved for user: " + profile.getEid());
} else {
log.error("Couldn't save official image for user: " + profile.getEid());
}
}
}
disableSecurityAdvisor(securityAdvisor);
}Example 4
| Project: sakai-cle-master File: ProfileConverter.java View source code |
/**
* Import profiles from the given CSV file
*
* <p>The CSV file may contain any of the following headings, in any order:
*
* <ul>
* <li>eid</li>
* <li>nickname</li>
* <li>position</li>
* <li>department</li>
* <li>school</li>
* <li>room</li>
* <li>web site</li>
* <li>work phone</li>
* <li>home phone</li>
* <li>mobile phone</li>
* <li>fax</li>
* <li>books</li>
* <li>tv</li>
* <li>movies</li>
* <li>quotes</li>
* <li>summary</li>
* <li>course</li>
* <li>subjects</li>
* <li>staff profile</li>
* <li>uni profile url</li>
* <li>academic profile url</li>
* <li>publications</li>
* <li>official image url</li>
* </ul>
*
* <p>Column headings must match EXACTLY the list above. They do not need to be in the same order, or even all present.
*
* <p>Fields must be comma separated and each field surrounded with double quotes. There must be no spaces between fields.
*
* <p>Only users that do not currently have a profile will be imported.
*
* @param path path to CSV file on the server
*/
public void importProfiles(String path) {
if (StringUtils.isBlank(path)) {
log.warn("Profile2 importer: invalid path to CSV file. Aborting.");
return;
}
HeaderColumnNameTranslateMappingStrategy<ImportableUserProfile> strat = new HeaderColumnNameTranslateMappingStrategy<ImportableUserProfile>();
strat.setType(ImportableUserProfile.class);
//map the column headers to the field names in the UserProfile class
//this mapping is not exhaustive and can be added to at any time since we are mapping
//on column name not position
Map<String, String> map = new HashMap<String, String>();
map.put("eid", "eid");
map.put("nickname", "nickname");
map.put("position", "position");
map.put("department", "department");
map.put("school", "school");
map.put("room", "room");
map.put("web site", "homepage");
map.put("work phone", "workphone");
map.put("home phone", "homephone");
map.put("mobile phone", "mobilephone");
map.put("fax", "facsimile");
map.put("books", "favouriteBooks");
map.put("tv", "favouriteTvShows");
map.put("movies", "favouriteMovies");
map.put("quotes", "favouriteQuotes");
map.put("summary", "personalSummary");
map.put("course", "course");
map.put("subjects", "subjects");
map.put("staff profile", "staffProfile");
map.put("uni profile url", "universityProfileUrl");
map.put("academic profile url", "academicProfileUrl");
map.put("publications", "publications");
map.put("official image url", "officialImageUrl");
strat.setColumnMapping(map);
CsvToBean<ImportableUserProfile> csv = new CsvToBean<ImportableUserProfile>();
List<ImportableUserProfile> list = new ArrayList<ImportableUserProfile>();
try {
list = csv.parse(strat, new CSVReader(new FileReader(path)));
} catch (FileNotFoundException fnfe) {
log.error("Profile2 importer: Couldn't find file: " + fnfe.getClass() + " : " + fnfe.getMessage());
}
//setup a security advisor so we can save profiles
SecurityAdvisor securityAdvisor = new SecurityAdvisor() {
public SecurityAdvice isAllowed(String userId, String function, String reference) {
return SecurityAdvice.ALLOWED;
}
};
enableSecurityAdvisor(securityAdvisor);
//process each
for (ImportableUserProfile profile : list) {
log.info("Processing user: " + profile.getEid());
//get uuid
String uuid = sakaiProxy.getUserIdForEid(profile.getEid());
if (StringUtils.isBlank(uuid)) {
log.error("Invalid user: " + profile.getEid() + ". Skipping...");
continue;
}
profile.setUserUuid(uuid);
//check if user already has a profile. Skip if so.
if (hasPersistentProfile(uuid)) {
log.warn("User: " + profile.getEid() + " already has a profile. Skipping...");
continue;
}
//persist user profile
try {
SakaiPerson sp = transformUserProfileToSakaiPerson(profile);
if (sp == null) {
//already logged
continue;
}
if (sakaiProxy.updateSakaiPerson(sp)) {
log.info("Profile saved for user: " + profile.getEid());
} else {
log.error("Couldn't save profile for user: " + profile.getEid());
continue;
}
} catch (ProfileNotDefinedException pnde) {
continue;
}
//add/update official image, if supplied in the CSV
if (StringUtils.isNotBlank(profile.getOfficialImageUrl())) {
if (imageLogic.saveOfficialImageUrl(uuid, profile.getOfficialImageUrl())) {
log.info("Official image saved for user: " + profile.getEid());
} else {
log.error("Couldn't save official image for user: " + profile.getEid());
}
}
}
disableSecurityAdvisor(securityAdvisor);
}