001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.module.cam.batch.service.impl;
017    
018    import java.io.File;
019    import java.io.FileInputStream;
020    import java.io.FileNotFoundException;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.util.Date;
024    import java.util.LinkedHashMap;
025    import java.util.Map;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.kuali.kfs.module.cam.batch.AssetBarcodeInventoryInputFileType;
029    import org.kuali.kfs.module.cam.batch.service.AssetBarcodeInventoryInputFileService;
030    import org.kuali.kfs.module.cam.document.web.struts.AssetBarCodeInventoryInputFileForm;
031    import org.kuali.kfs.sys.KFSConstants;
032    import org.kuali.kfs.sys.batch.BatchInputFileSetType;
033    import org.kuali.kfs.sys.batch.service.impl.BatchInputFileSetServiceImpl;
034    import org.kuali.kfs.sys.context.SpringContext;
035    import org.kuali.kfs.sys.exception.FileStorageException;
036    import org.kuali.rice.kim.bo.Person;
037    import org.kuali.rice.kns.exception.AuthorizationException;
038    import org.kuali.rice.kns.exception.ValidationException;
039    import org.kuali.rice.kns.service.DateTimeService;
040    
041    public class AssetBarcodeInventoryInputFileServiceImpl extends BatchInputFileSetServiceImpl implements  AssetBarcodeInventoryInputFileService {
042        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetBarcodeInventoryInputFileServiceImpl.class);
043    
044        public Map<String, String> save(Person user, AssetBarcodeInventoryInputFileType inputType, String fileUserIdentifier, Map<String, InputStream> typeToStreamMap, AssetBarCodeInventoryInputFileForm form) throws AuthorizationException, FileStorageException {
045            Date creationDate = SpringContext.getBean(DateTimeService.class).getCurrentDate();
046            Map<String, File> typeToTempFiles = copyStreamsToTemporaryDirectory(user, inputType, fileUserIdentifier, typeToStreamMap, creationDate);
047            
048            // null the map, because it's full of exhausted input streams that are useless 
049            typeToStreamMap = null;
050            
051            if (!inputType.validate(typeToTempFiles)) {
052                deleteTempFiles(typeToTempFiles);
053                LOG.error("Upload file validation failed for user " + user.getName() + " identifier " + fileUserIdentifier);
054                throw new ValidationException("File validation failed");
055            }
056            
057            byte[] buf = new byte[1024];
058    
059            Map<String, String> typeToFileNames = new LinkedHashMap<String, String>();
060            Map<String, File> typeToFiles = new LinkedHashMap<String, File>();
061            try {
062                for (String fileType : inputType.getFileTypes()) {
063                    File tempFile = typeToTempFiles.get(fileType);
064                    String saveFileName = inputType.getDirectoryPath(fileType) + File.separator + tempFile.getName();
065                    try {
066                        InputStream fileContents = new FileInputStream(tempFile);
067                        File fileToSave = new File(saveFileName);
068        
069                        copyInputStreamToFile(fileContents, fileToSave, buf);
070                        fileContents.close();
071                        typeToFileNames.put(fileType, saveFileName);
072                        typeToFiles.put(fileType, fileToSave);
073                    }
074                    catch (IOException e) {
075                        LOG.error("unable to save contents to file " + saveFileName, e);
076                        throw new RuntimeException("errors encountered while writing file " + saveFileName, e);
077                    }
078                }
079            }
080            finally {
081                deleteTempFiles(typeToTempFiles);
082            }
083    
084            String doneFileName = inputType.getDoneFileDirectoryPath() + File.separator + inputType.getDoneFileName(user, fileUserIdentifier, creationDate);
085            File doneFile = new File(doneFileName);
086            try {
087                doneFile.createNewFile();
088                
089                typeToFiles.put(KFSConstants.DONE_FILE_TYPE, doneFile);
090            }
091            catch (IOException e) {
092                LOG.error("unable to create done file", e);
093                throw new RuntimeException("unable to create done file", e);
094            }
095            
096            inputType.process(typeToFiles,form);
097            
098            return typeToFileNames;
099        }
100    }
101