* Spago SourceBean response
* @throws EMFUserError
*/
private void importConf(SourceBean request, SourceBean response) throws EMFUserError {
logger.debug("IN");
IImportManager impManager = null;
// get exported file and eventually the associations file
// UploadedFile archive = null;
// UploadedFile associationsFile = null;
FileItem archive = null;
FileItem associationsFileItem = null;
UploadedFile associationsFile = null;
AssociationFile assFile = null;
try {
String assKindFromReq = (String) request.getAttribute("importAssociationKind");
boolean isNoAssociationModality = assKindFromReq != null && assKindFromReq.equalsIgnoreCase("noassociations");
List uplFiles = request.getAttributeAsList("UPLOADED_FILE");
Iterator uplFilesIter = uplFiles.iterator();
while (uplFilesIter.hasNext()) {
//UploadedFile uplFile = (UploadedFile) uplFilesIter.next();
FileItem uplFile = (FileItem) uplFilesIter.next();
//String nameInForm = uplFile.getFieldNameInForm();
String nameInForm = uplFile.getFieldName();
if (nameInForm.equals("exportedArchive")) {
archive = uplFile;
} else if (nameInForm.equals("associationsFile")) {
associationsFileItem = uplFile;
}
}
// check that the name of the uploaded archive is not empty
//String archiveName = archive.getFileName();
String archiveName = GeneralUtilities.getRelativeFileNames(archive.getName());
if (archiveName.trim().equals("")) {
logger.error("Missing exported file");
response.setAttribute(ImportExportConstants.PUBLISHER_NAME, "ImportExportLoopbackStopImport");
throw new EMFValidationError(EMFErrorSeverity.ERROR, "exportedArchive", "8007", "component_impexp_messages");
}
int maxSize = ImportUtilities.getImportFileMaxSize();
if (archive.getSize() > maxSize) {
logger.error("File is too large!!!");
response.setAttribute(ImportExportConstants.PUBLISHER_NAME, "ImportExportLoopbackStopImport");
throw new EMFValidationError(EMFErrorSeverity.ERROR, "exportedArchive", "202");
}
// checks if the association file is bigger than 1 MB, that is more than enough!!
if (associationsFileItem != null) {
if (associationsFileItem.getSize() > 1048576) {
throw new EMFValidationError(EMFErrorSeverity.ERROR, "associationsFile", "202");
}
// loads association file
associationsFile = new UploadedFile();
associationsFile.setFileContent(associationsFileItem.get());
associationsFile.setFieldNameInForm(associationsFileItem.getFieldName());
associationsFile.setSizeInBytes(associationsFileItem.getSize());
associationsFile.setFileName(GeneralUtilities.getRelativeFileNames(associationsFileItem.getName()));
}
// if the user choose to have no associations, checks the form, otherwise set the variable associationsFile = null
if (!isNoAssociationModality) {
// check if the name of associations file is empty (in this case set
// null to the variable)
if (associationsFile != null) {
String associationsFileName = associationsFile.getFileName();
if (associationsFileName.trim().equals("")) {
associationsFile = null;
}
}
// if the association file is empty then check if there is an
// association id
// rebuild the uploaded file and assign it to associationsFile variable
if (associationsFile == null) {
String assId = (String) request.getAttribute("hidAssId");
if ((assId != null) && !assId.trim().equals("")) {
IAssociationFileDAO assfiledao = new AssociationFileDAO();
assFile = assfiledao.loadFromID(assId);
byte[] content = assfiledao.getContent(assFile);
UploadedFile uplFile = new UploadedFile();
uplFile.setSizeInBytes(content.length);
uplFile.setFileContent(content);
uplFile.setFileName("association.xml");
uplFile.setFieldNameInForm("");
associationsFile = uplFile;
}
}
} else {
associationsFile = null;
}
// get the association mode
String assMode = IImportManager.IMPORT_ASS_DEFAULT_MODE;
if (assKindFromReq.equalsIgnoreCase("predefinedassociations")) {
assMode = IImportManager.IMPORT_ASS_PREDEFINED_MODE;
}
// get bytes of the archive
byte[] archiveBytes = archive.get();
// get path of the import tmp directory
String pathImpTmpFolder = ImportUtilities.getImportTempFolderPath();
// apply transformation
TransformManager transManager = new TransformManager();
archiveBytes = transManager.applyTransformations(archiveBytes, archiveName, pathImpTmpFolder);
impManager = ImportUtilities.getImportManagerInstance();
// prepare import environment
impManager.init(pathImpTmpFolder, archiveName, archiveBytes);
impManager.openSession();
impManager.setAssociationFile(assFile);
// if the associations file has been uploaded fill the association keeper
if(associationsFile!=null) {
byte[] assFilebys = associationsFile.getFileContent();
String assFileStr = new String(assFilebys);
try {
impManager.getUserAssociation().fillFromXml(assFileStr);
} catch (Exception e) {
logger.error("Error while loading association file content:\n " + e);
response.setAttribute(ImportExportConstants.PUBLISHER_NAME, "ImportExportLoopbackStopImport");
throw new EMFValidationError(EMFErrorSeverity.ERROR, "exportedArchive", "8009", "component_impexp_messages");
}
}
// set into import manager the association import mode
impManager.setImpAssMode(assMode);
RequestContainer requestContainer = this.getRequestContainer();
SessionContainer session = requestContainer.getSessionContainer();
session.setAttribute(ImportExportConstants.IMPORT_MANAGER, impManager);
// start import operations
if (impManager.getImpAssMode().equals(IImportManager.IMPORT_ASS_PREDEFINED_MODE) && !impManager.associateAllExportedRolesByUserAssociation()) {
response.setAttribute(ImportExportConstants.PUBLISHER_NAME, "ImportExportSkipRoleAssociation");
} else {
// move to jsp
List exportedRoles = impManager.getExportedRoles();
IRoleDAO roleDAO = DAOFactory.getRoleDAO();
List currentRoles = roleDAO.loadAllRoles();
response.setAttribute(ImportExportConstants.LIST_EXPORTED_ROLES, exportedRoles);
response.setAttribute(ImportExportConstants.LIST_CURRENT_ROLES, currentRoles);
response.setAttribute(ImportExportConstants.PUBLISHER_NAME, "ImportExportRoleAssociation");
}
} catch (EMFUserError emfue) {
if (impManager != null)
impManager.stopImport();
throw emfue;
} catch (ClassNotFoundException cnde) {
logger.error("Importer class not found", cnde);
if (impManager != null)
impManager.stopImport();
throw new EMFUserError(EMFErrorSeverity.ERROR, "8004", "component_impexp_messages");
} catch (InstantiationException ie) {
logger.error("Cannot create an instance of importer class ", ie);
if (impManager != null)
impManager.stopImport();
throw new EMFUserError(EMFErrorSeverity.ERROR, "8004", "component_impexp_messages");
} catch (IllegalAccessException iae) {
logger.error("Cannot create an instance of importer class ", iae);
if (impManager != null)
impManager.stopImport();
throw new EMFUserError(EMFErrorSeverity.ERROR, "8004", "component_impexp_messages");
} catch (SourceBeanException sbe) {
logger.error("Error: " + sbe);
if (impManager != null)
impManager.stopImport();
throw new EMFUserError(EMFErrorSeverity.ERROR, "8004", "component_impexp_messages");
} catch (Exception e) {
logger.error(e);
if (impManager != null)
impManager.stopImport();
throw new EMFUserError(EMFErrorSeverity.ERROR, "8004", "component_impexp_messages");
} finally {
if (impManager != null)
impManager.closeSession();
logger.debug("OUT");
}
}