001: package com.technoetic.xplanner.actions;
002:
003: import java.io.BufferedReader;
004: import java.io.InputStream;
005: import java.io.InputStreamReader;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008: import java.util.List;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import net.sf.hibernate.Hibernate;
013: import net.sf.hibernate.HibernateException;
014: import net.sf.hibernate.Session;
015: import org.apache.commons.lang.StringUtils;
016: import org.apache.log4j.Logger;
017: import org.apache.struts.action.ActionForm;
018: import org.apache.struts.action.ActionForward;
019: import org.apache.struts.action.ActionMapping;
020: import org.apache.struts.upload.FormFile;
021:
022: import com.technoetic.xplanner.domain.Person;
023: import com.technoetic.xplanner.domain.repository.DuplicateUserIdException;
024: import com.technoetic.xplanner.domain.repository.ObjectRepository;
025: import com.technoetic.xplanner.forms.ImportPeopleForm;
026: import com.technoetic.xplanner.security.auth.AuthorizationException;
027: import com.technoetic.xplanner.util.Callable;
028:
029: /**
030: * User: Mateusz Prokopowicz
031: * Date: Dec 2, 2004
032: * Time: 5:59:21 PM
033: */
034: public class ImportPeopleAction extends AbstractAction {
035: private static final int NBR_OF_COLUMNS = 5;
036: private static final String STATUS_SUCCESS_KEY = "people.import.status.success";
037: private static final String STATUS_WRONG_ENTRY_KEY = "people.import.status.wrong_entry_format";
038: private static final String STATUS_EMPTY_USERID_KEY = "people.import.status.empty_userId";
039: private static final String STATUS_USERID_EXISTS_KEY = "people.import.status.userId_exists";
040: static final String EDIT_PERSON_ACTION_NAME = "/edit/person";
041:
042: public void setEditPersonHelper(EditPersonHelper editPersonHelper) {
043: }
044:
045: public ObjectRepository getPersonRepository() {
046: return getMetaRepository().getRepository(Person.class);
047: }
048:
049: public ActionForward execute(ActionMapping mapping,
050: ActionForm form, HttpServletRequest request,
051: HttpServletResponse response) throws Exception {
052: ImportPeopleForm importForm = (ImportPeopleForm) form;
053: FormFile formFile = importForm.getFormFile();
054: if (formFile != null) {
055: String filename = formFile.getFileName();
056: if (StringUtils.isNotEmpty(filename)) {
057: String contentType = formFile.getContentType();
058: InputStream input = formFile.getInputStream();
059: int fileSize = formFile.getFileSize();
060: BufferedReader importReader = new BufferedReader(
061: new InputStreamReader(input));
062: for (String line = importReader.readLine(); line != null; line = importReader
063: .readLine()) {
064: List entry = new ArrayList();
065: String status = null;
066: String id = "";
067: entry.addAll(Arrays.asList(line.split(",")));
068: try {
069: if (entry.size() < NBR_OF_COLUMNS) {
070: throw new PeopleImportException(
071: STATUS_WRONG_ENTRY_KEY);
072: }
073: if (StringUtils.isEmpty((String) entry.get(0))) {
074: throw new PeopleImportException(
075: STATUS_EMPTY_USERID_KEY);
076: }
077: final Person person = new Person();
078: person.setUserId((String) entry.get(0));
079: person.setName((String) entry.get(1));
080: person.setEmail((String) entry.get(2));
081: person.setInitials((String) entry.get(3));
082: person.setPhone((String) entry.get(4));
083: id = (String) transactionTemplate
084: .execute(new Callable() {
085: public Object run()
086: throws Exception {
087: return ""
088: + getPersonRepository()
089: .insert(person);
090: }
091: });
092: status = STATUS_SUCCESS_KEY;
093:
094: } catch (DuplicateUserIdException duie) {
095: status = STATUS_USERID_EXISTS_KEY;
096: } catch (PeopleImportException ex) {
097: status = ex.getMessage();
098: } catch (AuthorizationException e) {
099: request.setAttribute("exception", e);
100: return mapping
101: .findForward("security/notAuthorized");
102: } finally {
103: importForm.addResult(id, (String) entry.get(0),
104: (String) entry.get(1), status);
105: }
106: }
107: Logger.getLogger(ImportPeopleAction.class).debug(
108: "Importing people: filename=" + filename
109: + ", fileSize=" + fileSize
110: + ", contentType=" + contentType);
111: }
112: }
113: return new ActionForward(mapping.getInput());
114: }
115:
116: protected ActionForward doExecute(ActionMapping mapping,
117: ActionForm form, HttpServletRequest request,
118: HttpServletResponse response) throws Exception {
119: return null;
120: }
121:
122: class PeopleImportException extends Exception {
123: public PeopleImportException(String msg) {
124: super(msg);
125: }
126: }
127: }
|