001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.gl.batch.collector;
017:
018: import java.io.File;
019: import java.sql.Timestamp;
020: import java.text.FieldPosition;
021: import java.text.SimpleDateFormat;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.kuali.core.bo.user.UniversalUser;
025: import org.kuali.core.service.DateTimeService;
026: import org.kuali.kfs.KFSConstants;
027: import org.kuali.kfs.KFSKeyConstants;
028: import org.kuali.kfs.batch.BatchInputFileTypeBase;
029: import org.kuali.module.gl.service.CollectorHelperService;
030:
031: /**
032: * Batch input type for the collector job.
033: */
034: public class CollectorInputFileType extends BatchInputFileTypeBase {
035: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036: .getLogger(CollectorInputFileType.class);
037:
038: private DateTimeService dateTimeService;
039: private CollectorHelperService collectorHelperService;
040:
041: /**
042: * Returns the identifier of the Collector's file type
043: *
044: * @return the Collector's file type identifier
045: * @see org.kuali.kfs.batch.BatchInputFileType#getFileTypeIdentifer()
046: */
047: public String getFileTypeIdentifer() {
048: return KFSConstants.COLLECTOR_FILE_TYPE_INDENTIFIER;
049: }
050:
051: /**
052: * Returns the class associated with the authorization workgroup for the input type, in this case CollectorStep
053: *
054: * @return the CollectorStep class
055: * @see org.kuali.kfs.batch.BatchInputType#getUploadWorkgroupParameterComponent()
056: */
057: public Class getUploadWorkgroupParameterComponent() {
058: return CollectorStep.class;
059: }
060:
061: /**
062: * Builds the file name using the following construction: All collector files start with gl_idbilltrans_ append the chartorg
063: * from the batch header append the username of the user who is uploading the file then the user supplied indentifier finally
064: * the timestamp
065: *
066: * @param user who uploaded the file
067: * @param parsedFileContents represents collector batch object
068: * @param userIdentifier user identifier for user who uploaded file
069: * @return String returns file name using the convention mentioned in the description
070: *
071: * @see org.kuali.kfs.batch.BatchInputFileType#getFileName(org.kuali.core.bo.user.UniversalUser, java.lang.Object,
072: * java.lang.String)
073: */
074: public String getFileName(UniversalUser user,
075: Object parsedFileContents, String userIdentifier) {
076: CollectorBatch collectorBatch = (CollectorBatch) parsedFileContents;
077: Timestamp currentTimestamp = dateTimeService
078: .getCurrentTimestamp();
079:
080: StringBuffer buf = new StringBuffer();
081: SimpleDateFormat formatter = new SimpleDateFormat(
082: "yyyyMMdd_HHmmss");
083: formatter.setLenient(false);
084: formatter.format(currentTimestamp, buf, new FieldPosition(0));
085:
086: String fileName = "gl_idbilltrans_"
087: + collectorBatch.getChartOfAccountsCode()
088: + collectorBatch.getOrganizationCode();
089: fileName += "_" + user.getPersonUserIdentifier().toLowerCase();
090: if (StringUtils.isNotBlank(userIdentifier)) {
091: fileName += "_" + userIdentifier;
092: }
093: fileName += "_" + buf.toString();
094:
095: // remove spaces in filename
096: fileName = StringUtils.remove(fileName, " ");
097:
098: return fileName;
099: }
100:
101: /**
102: * Verifies user created the file by checking for the username in the file name.
103: *
104: * @param user user who created file
105: * @param batchFile uploaded batch file
106: * @return true if user's username in in file
107: *
108: * @see org.kuali.kfs.batch.BatchInputFileType#checkAuthorization(org.kuali.core.bo.user.UniversalUser, java.io.File)
109: */
110: public boolean checkAuthorization(UniversalUser user, File batchFile) {
111: boolean isAuthorized = false;
112:
113: String userIdentifier = user.getPersonUserIdentifier();
114: userIdentifier = StringUtils.remove(userIdentifier, " ");
115:
116: String[] fileNameParts = StringUtils.split(batchFile.getName(),
117: "_");
118: if (fileNameParts.length > 4) {
119: if (fileNameParts[3].equalsIgnoreCase(userIdentifier
120: .toLowerCase())) {
121: isAuthorized = true;
122: }
123: }
124:
125: return isAuthorized;
126: }
127:
128: /**
129: * Checks that the file contents parsed from the file are valid Collector data
130: *
131: * @param parsedFileContents represents collector batch
132: * @return true if valid, false if not
133: * @see org.kuali.kfs.batch.BatchInputFileType#validate(java.lang.Object)
134: */
135: public boolean validate(Object parsedFileContents) {
136: boolean isValid = collectorHelperService
137: .performValidation((CollectorBatch) parsedFileContents);
138: if (isValid) {
139: isValid = collectorHelperService.checkTrailerTotals(
140: (CollectorBatch) parsedFileContents, null);
141: }
142:
143: return isValid;
144: }
145:
146: /**
147: * Returns the Collector's title key
148: *
149: * @return the title key for the Collector
150: * @see org.kuali.kfs.batch.BatchInputFileType#getTitleKey()
151: */
152: public String getTitleKey() {
153: return KFSKeyConstants.MESSAGE_BATCH_UPLOAD_TITLE_COLLECTOR;
154: }
155:
156: /**
157: * Sets the dateTimeService attribute value.
158: */
159: public void setDateTimeService(DateTimeService dateTimeService) {
160: this .dateTimeService = dateTimeService;
161: }
162:
163: /**
164: * Sets the collectorService attribute value.
165: */
166: public void setCollectorHelperService(
167: CollectorHelperService collectorHelperService) {
168: this.collectorHelperService = collectorHelperService;
169: }
170:
171: }
|