001: /*
002: * Created by IntelliJ IDEA.
003: * User: sg426575
004: * Date: May 12, 2005
005: * Time: 11:08:51 PM
006: */
007: package com.technoetic.xplanner.actions;
008:
009: import java.io.IOException;
010: import javax.servlet.http.Cookie;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import org.apache.struts.action.*;
015:
016: import com.technoetic.xplanner.XPlannerProperties;
017: import com.technoetic.xplanner.domain.Iteration;
018: import com.technoetic.xplanner.forms.AbstractEditorForm;
019: import com.technoetic.xplanner.forms.ImportStoriesForm;
020: import com.technoetic.xplanner.importer.MissingColumnHeaderSpreadsheetImporterException;
021: import com.technoetic.xplanner.importer.MissingFieldSpreadsheetImporterException;
022: import com.technoetic.xplanner.importer.SpreadsheetStoryImporter;
023: import com.technoetic.xplanner.importer.WrongImportFileSpreadsheetImporterException;
024: import com.technoetic.xplanner.importer.spreadsheet.MissingWorksheetException;
025: import com.technoetic.xplanner.importer.spreadsheet.SpreadsheetHeaderConfiguration;
026: import com.technoetic.xplanner.util.CookieSupport;
027:
028: public class ImportStoriesAction extends EditObjectAction {
029: SpreadsheetStoryImporter importer;
030: public static final String WORKSHEET_NAME_PROPERTY_KEY = "import.spreadsheet.worksheet.name";
031: public static final String STORY_TITLE_PROPERTY_KEY = "import.spreadsheet.story.title.column.header";
032: public static final String STORY_ESTIMATE_PROPERTY_KEY = "import.spreadsheet.story.estimate.column.header";
033: public static final String ITERATION_END_DATE_PROPERTY_KEY = "import.spreadsheet.iteration.end.date.column.header";
034: public static final String STORY_PRIORITY_PROPERTY_KEY = "import.spreadsheet.story.priority.column.header";
035: public static final String STORY_STATUS_PROPERTY_KEY = "import.spreadsheet.story.status.column.header";
036: public final static String ONLY_INCOMPLETE_COOKIE_NAME = "import.spreadsheet.onlyIncomplete";
037: public final static String COMPLETED_STORY_STATUS_KEY = "import.spreadsheet.completedStoryStatus";
038:
039: public ActionForward execute(ActionMapping mapping,
040: ActionForm actionForm, HttpServletRequest request,
041: HttpServletResponse response) throws Exception {
042: ActionErrors errors = new ActionErrors();
043: try {
044: return super
045: .execute(mapping, actionForm, request, response);
046: } catch (WrongImportFileSpreadsheetImporterException e) {
047: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
048: "import.status.corrupted_file"));
049: } catch (MissingFieldSpreadsheetImporterException e) {
050: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
051: "import.status.missing_required_field", e
052: .getField()));
053: } catch (MissingColumnHeaderSpreadsheetImporterException e) {
054: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
055: "import.status.wrong_header", e.getColumnName()));
056: } catch (MissingWorksheetException e) {
057: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
058: "import.status.worksheet_not_found", e
059: .getWorksheetName()));
060: }
061: saveErrors(request, errors);
062: return mapping.getInputForward();
063: }
064:
065: protected void populateObject(HttpServletRequest request,
066: Object object, ActionForm form) throws IOException {
067: ImportStoriesForm importStoriesForm = (ImportStoriesForm) form;
068: SpreadsheetHeaderConfiguration headerConfiguration = new SpreadsheetHeaderConfiguration();
069: populateHeaderConfiguration(headerConfiguration,
070: importStoriesForm);
071: importStoriesForm.setResults(importer.importStories(
072: (Iteration) object, headerConfiguration,
073: importStoriesForm.getFormFile().getInputStream(),
074: importStoriesForm.isOnlyIncomplete()));
075: }
076:
077: private void populateHeaderConfiguration(
078: SpreadsheetHeaderConfiguration headerConfiguration,
079: ImportStoriesForm form) {
080: headerConfiguration.setWorksheetName(form.getWorksheetName());
081: headerConfiguration.setTitleHeader(form.getTitleColumn());
082: headerConfiguration.setEndDateHeader(form.getEndDateColumn());
083: headerConfiguration.setEstimateHeader(form.getEstimateColumn());
084: headerConfiguration.setPriorityHeader(form.getPriorityColumn());
085: headerConfiguration.setStatusHeader(form.getStatusColumn());
086: }
087:
088: protected void setCookies(AbstractEditorForm form,
089: ActionMapping mapping, HttpServletRequest request,
090: HttpServletResponse response) {
091: ImportStoriesForm importForm = (ImportStoriesForm) form;
092: addCookie(WORKSHEET_NAME_PROPERTY_KEY, importForm
093: .getWorksheetName(), response);
094: addCookie(STORY_TITLE_PROPERTY_KEY,
095: importForm.getTitleColumn(), response);
096: addCookie(ITERATION_END_DATE_PROPERTY_KEY, importForm
097: .getEndDateColumn(), response);
098: addCookie(STORY_ESTIMATE_PROPERTY_KEY, importForm
099: .getEstimateColumn(), response);
100: addCookie(STORY_PRIORITY_PROPERTY_KEY, importForm
101: .getPriorityColumn(), response);
102: addCookie(STORY_STATUS_PROPERTY_KEY, importForm
103: .getStatusColumn(), response);
104: addCookie(ONLY_INCOMPLETE_COOKIE_NAME, ""
105: + importForm.isOnlyIncomplete(), response);
106: addCookie(COMPLETED_STORY_STATUS_KEY, importForm
107: .getCompletedStatus(), response);
108: }
109:
110: private void addCookie(String propertyKey, String value,
111: HttpServletResponse response) {
112: CookieSupport.createCookie(propertyKey, value, response);
113: }
114:
115: protected void populateForm(AbstractEditorForm actionForm,
116: ActionMapping actionMapping, HttpServletRequest request) {
117: ImportStoriesForm form = (ImportStoriesForm) actionForm;
118: form.setWorksheetName(getValueFromCookieOrProperties(
119: WORKSHEET_NAME_PROPERTY_KEY, request));
120: form.setTitleColumn(getValueFromCookieOrProperties(
121: STORY_TITLE_PROPERTY_KEY, request));
122: form.setEndDateColumn(getValueFromCookieOrProperties(
123: ITERATION_END_DATE_PROPERTY_KEY, request));
124: form.setPriorityColumn(getValueFromCookieOrProperties(
125: STORY_PRIORITY_PROPERTY_KEY, request));
126: form.setEstimateColumn(getValueFromCookieOrProperties(
127: STORY_ESTIMATE_PROPERTY_KEY, request));
128: form.setStatusColumn(getValueFromCookieOrProperties(
129: STORY_STATUS_PROPERTY_KEY, request));
130: form.setOnlyIncomplete(Boolean.valueOf(
131: getValueFromCookieOrProperties(
132: ONLY_INCOMPLETE_COOKIE_NAME, request))
133: .booleanValue());
134: form.setCompletedStatus(getValueFromCookieOrProperties(
135: COMPLETED_STORY_STATUS_KEY, request));
136: }
137:
138: public SpreadsheetStoryImporter getImporter() {
139: return importer;
140: }
141:
142: public void setImporter(SpreadsheetStoryImporter importer) {
143: this .importer = importer;
144: }
145:
146: public String getValueFromCookieOrProperties(String key,
147: HttpServletRequest request) {
148: Cookie cookie = CookieSupport.getCookie(key, request);
149: if (cookie != null) {
150: return cookie.getValue();
151: }
152: XPlannerProperties xplannerProperties = new XPlannerProperties();
153: return xplannerProperties.getProperty(key);
154: }
155:
156: }
|