diff --git a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/AmpFundingAmountComponent.java b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/AmpFundingAmountComponent.java index 5ee3f1f9088..8d0e0c622b5 100644 --- a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/AmpFundingAmountComponent.java +++ b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/AmpFundingAmountComponent.java @@ -50,7 +50,7 @@ /** * Reusable component capturing an amount item in AMP (the tuple amount / * currency / date ) - * + * * @author mpostelnicu@dgateway.org since Nov 2, 2010 */ public class AmpFundingAmountComponent extends Panel { @@ -70,17 +70,17 @@ public List getObject() { int startYear = FeaturesUtil.getGlobalSettingValueInteger(GlobalSettingsConstants.YEAR_RANGE_START); int range = FeaturesUtil.getGlobalSettingValueInteger(GlobalSettingsConstants.NUMBER_OF_YEARS_IN_RANGE); boolean fiscal = MTEFYearsModel.getFiscal(); - + list = new ArrayList(range); - + Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_YEAR, 1); - + for (int i = 0; i < range; i++) { calendar.set(Calendar.YEAR, startYear + i); list.add(MTEFYearsModel.convert(calendar.getTime(), fiscal)); } - + return list; } }; @@ -93,7 +93,7 @@ public AmpFundingAmountComponent(String id, IModel model, String fmAmount, this(id, model, fmAmount, propertyAmount, fmCurrency, propertyCurrency, fmDate, propertyDate, isMTEFProjection, false,null); } - + public AmpFundingAmountComponent(String id, IModel model, String fmAmount, String propertyAmount, String fmCurrency, String propertyCurrency, String fmDate, String propertyDate, boolean isMTEFProjection,String amountSize) { @@ -101,7 +101,7 @@ public AmpFundingAmountComponent(String id, IModel model, String fmAmount, isMTEFProjection, false,amountSize); } - + protected AmpFundingAmountComponent(String id, IModel model, String fmAmount, String propertyAmount, String fmCurrency, String propertyCurrency, String fmDate, String propertyDate, boolean isMTEFProjection, boolean fundingComponentTableMode,String amountSize) { @@ -112,16 +112,16 @@ protected AmpFundingAmountComponent(String id, IModel model, String fmAmount, this.isMTEFProjection=isMTEFProjection; amount = new AmpTextFieldPanel("amount", new PropertyModel(model, propertyAmount), fmAmount, hideLabel, hideNewLine) { - + @Override protected void onAjaxOnUpdate(final AjaxRequestTarget target) { AmpComponentPanel parentPanel = findParent(AmpFundingItemFeaturePanel.class); if(parentPanel ==null) - parentPanel = findParent(AmpComponentField.class); + parentPanel = findParent(AmpComponentField.class); if(parentPanel ==null) parentPanel = findParent(AmpRegionalFundingItemFeaturePanel.class); if(parentPanel==null) - parentPanel = findParent(AmpComponentFormTableAnnualBudget.class); + parentPanel = findParent(AmpComponentFormTableAnnualBudget.class); parentPanel.visitChildren(AmpCollectionValidatorField.class, new IVisitor() { @Override public void component(AmpCollectionValidatorField component, @@ -130,20 +130,20 @@ public void component(AmpCollectionValidatorField component, visit.dontGoDeeper(); } }); - + onFundingDetailChanged(target); } - + public IConverter getInternalConverter(java.lang.Class type) { return CustomDoubleConverter.INSTANCE; } }; amount.getTextContainer().setRequired(true); if(amountSize==null) - amount.getTextContainer().add(new AttributeModifier("size", new Model("9"))); + amount.getTextContainer().add(new AttributeModifier("size", new Model<>("9"))); else - amount.getTextContainer().add(new AttributeModifier("size", new Model(amountSize))); + amount.getTextContainer().add(new AttributeModifier("size", new Model<>(amountSize))); add(amount); @@ -225,14 +225,14 @@ public void component(AmpSimpleValidatorField component, IVisit visit) { } else if (!FeaturesUtil.getGlobalSettingValueBoolean(GlobalSettingsConstants.MTEF_ANNUAL_DATE_FORMAT)) { AmpDatePickerFieldPanel datetmp = new AmpDatePickerFieldPanel( "date", new PropertyModel(model, propertyDate), fmDate, null, hideLabel, hideNewLine); - + datetmp.getDate().setRequired(true); datetmp.getDate().add(new AttributeModifier("class", "inputx_date")); date = datetmp; } else { MTEFYearsModel yearModel = new MTEFYearsModel(new PropertyModel(model, propertyDate)); - AmpSelectFieldPanel datetmp = new AmpSelectFieldPanel("date", yearModel, + AmpSelectFieldPanel datetmp = new AmpSelectFieldPanel("date", yearModel, mtefYearsChoices, fmDate, true, true, new ChoiceRenderer("value", "key")); date = datetmp; } @@ -243,10 +243,10 @@ public void component(AmpSimpleValidatorField component, IVisit visit) { setRenderBodyOnly(true); } - + /** * Method called when the amount field value has been changed - * + * * @param target */ protected void onFundingDetailChanged(AjaxRequestTarget target) { @@ -282,7 +282,7 @@ public Component getDate() { return date; } private void sendEvent(AjaxRequestTarget target){ - + } public void setAmountValidator(final AmpCollectionValidatorField validationHiddenField){ validationFields.add(validationHiddenField); @@ -292,7 +292,7 @@ public void setAmountValidator(final AmpCollectionValidatorField validationHidde protected void onUpdate(final AjaxRequestTarget target) { AmpComponentPanel parentPanel = findParent(AmpFundingItemFeaturePanel.class); if(parentPanel ==null) - parentPanel = findParent(AmpComponentField.class); + parentPanel = findParent(AmpComponentField.class); if(parentPanel ==null) parentPanel = findParent(AmpRegionalFundingItemFeaturePanel.class); parentPanel.visitChildren(AmpCollectionValidatorField.class, new IVisitor() { diff --git a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/ActivityUtil.java b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/ActivityUtil.java index bd5b999b3f9..d195b7ce6c8 100644 --- a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/ActivityUtil.java +++ b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/ActivityUtil.java @@ -99,8 +99,6 @@ import org.hibernate.LockOptions; import org.hibernate.Query; import org.hibernate.Session; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Restrictions; /** * Util class used to manipulate an activity @@ -202,12 +200,12 @@ public static void prepareToSave(AmpActivityVersion a, AmpActivityVersion oldA, setActivityStatus(ampCurrentMember, draft, a, oldA, newActivity, context.isRejected()); } } - + public static AmpActivityVersion saveActivityNewVersion(AmpActivityVersion a, Collection translations, List cumulativeTranslations, AmpTeamMember ampCurrentMember, boolean draft, Session session, SaveContext context, EditorStore editorStore, Site site) throws Exception { - + boolean draftChange = detectDraftChange(a, draft); return saveActivityNewVersion(a, translations, cumulativeTranslations, ampCurrentMember, draft, draftChange, session, context, editorStore, site); @@ -313,7 +311,7 @@ public static AmpActivityVersion saveActivityNewVersion(AmpActivityVersion a, saveAgreements(a, session, isActivityForm); saveContacts(a, session, (draft != draftChange), ampCurrentMember); - updateComponentFunding(a, session); + updateComponentFunding(a); saveAnnualProjectBudgets(a, session); saveProjectCosts(a, session); saveStructures(a, session); @@ -354,7 +352,7 @@ private static void updateIndirectPrograms(AmpActivityVersion a, Session session public static boolean detectDraftChange(AmpActivityVersion a, boolean draft) { return Boolean.TRUE.equals(a.getDraft()) != draft; } - + public static boolean isNewActivity(T a) { // it would be nicer to rely upon AMP ID, but some old activities may lack it return a.getAmpActivityId() == null; @@ -628,7 +626,7 @@ public static boolean canApprove(AmpTeamMember atm, Long activityTeamId, Approva } return false; } - + /** * An activity can be rejected only if: * 1. the activity is not new @@ -645,7 +643,7 @@ public static boolean canReject(AmpTeamMember atm, Boolean isDraft, Boolean isNe return BooleanUtils.isFalse(isNewActivity) && BooleanUtils.isFalse(isDraft) && isProjectValidationOn(getValidationSetting(atm)) && isApprover(atm); } - + /** * Detect if the teammember is approver of the workspace or is the teamlead of the ws * @@ -742,29 +740,18 @@ public static AmpActivityVersion load(AmpActivityModel am, Long id) { } - private static void updateComponentFunding(AmpActivityVersion a, Session session) { + private static void updateComponentFunding(AmpActivityVersion a) { Set components = a.getComponents(); if (components == null) { return; } - Iterator componentIterator = components.iterator(); - while (componentIterator.hasNext()) { - AmpComponent ampComponent = componentIterator.next(); + for (AmpComponent ampComponent : components) { + if (Hibernate.isInitialized(ampComponent.getFundings()) && (ampComponent.getFundings() != null)) { - if (Hibernate.isInitialized(ampComponent.getFundings())) { - if (ampComponent.getFundings() != null) { - Iterator ampComponentFundingsIterator = ampComponent.getFundings().iterator(); + ampComponent.getFundings().removeIf(acf -> acf.getTransactionAmount() == null); - while (ampComponentFundingsIterator.hasNext()) { - AmpComponentFunding acf = ampComponentFundingsIterator.next(); - - if (acf.getTransactionAmount() == null) { - ampComponentFundingsIterator.remove(); - } - } - } } } } @@ -777,18 +764,14 @@ private static void saveComments(AmpActivityVersion a, Session session, boolean HashSet delComm = s.getMetaData(OnePagerConst.COMMENTS_DELETED_ITEMS); if (delComm != null){ - Iterator di = delComm.iterator(); - while (di.hasNext()) { - AmpComments tComm = (AmpComments) di.next(); + for (AmpComments tComm : delComm) { session.delete(tComm); } } if (newComm != null){ - Iterator ni = newComm.iterator(); - while (ni.hasNext()) { - AmpComments tComm = (AmpComments) ni.next(); - if (ActivityVersionUtil.isVersioningEnabled() && !draft){ + for (AmpComments tComm : newComm) { + if (ActivityVersionUtil.isVersioningEnabled() && !draft) { try { tComm = (AmpComments) tComm.prepareMerge(a); } catch (CloneNotSupportedException e) { @@ -797,7 +780,7 @@ private static void saveComments(AmpActivityVersion a, Session session, boolean } if (tComm.getMemberId() == null) - tComm.setMemberId(((AmpAuthWebSession)org.apache.wicket.Session.get()).getAmpCurrentMember()); + tComm.setMemberId(((AmpAuthWebSession) org.apache.wicket.Session.get()).getAmpCurrentMember()); if (tComm.getAmpActivityId() == null) tComm.setAmpActivityId(a); session.saveOrUpdate(tComm); diff --git a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/SessionUtil.java b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/SessionUtil.java index d3f15ad4a38..90b4cf1f76e 100644 --- a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/SessionUtil.java +++ b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/util/SessionUtil.java @@ -46,8 +46,8 @@ public static void extendSessionIfNeeded(HttpServletRequest request) { // Extend session timeout if remaining time is 30 seconds or less if (remainingTime <= 30000) { // 30 seconds in milliseconds - // Extend session timeout to 130 minutes - session.setMaxInactiveInterval(1800); // 130 minutes * 60 seconds + // Extend session timeout to 30 minutes + session.setMaxInactiveInterval(1800); // 30 minutes * 60 seconds logger.info("Session timeout extended to "+session.getMaxInactiveInterval()); } } diff --git a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportStatus.java b/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportStatus.java deleted file mode 100644 index 602ed77d3a7..00000000000 --- a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportStatus.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.digijava.module.admin.dbentity; - -public enum ImportStatus { -UPLOADED,IN_PROGRESS,SUCCESS,FAILED -} diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/DbUtil.java b/amp/WEB-INF/src/org/digijava/module/admin/util/DbUtil.java index 4fb231901db..8c4c72a3054 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/DbUtil.java +++ b/amp/WEB-INF/src/org/digijava/module/admin/util/DbUtil.java @@ -22,12 +22,6 @@ package org.digijava.module.admin.util; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.StringTokenizer; - import org.apache.log4j.Logger; import org.dgfoundation.amp.error.AMPException; import org.digijava.kernel.ampapi.endpoints.config.utils.ConfigHelper; @@ -46,6 +40,8 @@ import org.hibernate.Session; import org.hibernate.Transaction; +import java.util.*; + public class DbUtil { private static Logger logger = Logger.getLogger(DbUtil.class); @@ -200,7 +196,7 @@ public static Group getGroupByKey(String key) throws logger.debug("Unable to get group from database ", ex); throw new AdminException("Unable to get group from database ", ex); } - return group; + return group; } public static void editGroup(Group group) throws AdminException { Session sess = null; diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Location.java b/amp/WEB-INF/src/org/digijava/module/admin/util/model/Location.java deleted file mode 100644 index a74d2078de1..00000000000 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Location.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.digijava.module.admin.util.model; - -public class Location { - private Long id; - private Long location; - private Double location_percentage; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getLocation() { - return location; - } - - public void setLocation(Long location) { - this.location = location; - } - - public Double getLocation_percentage() { - return location_percentage; - } - - public void setLocation_percentage(Double location_percentage) { - this.location_percentage = location_percentage; - } -} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/DataImporter.java b/amp/WEB-INF/src/org/digijava/module/aim/action/DataImporter.java deleted file mode 100644 index c66003a7ef5..00000000000 --- a/amp/WEB-INF/src/org/digijava/module/aim/action/DataImporter.java +++ /dev/null @@ -1,781 +0,0 @@ -package org.digijava.module.aim.action; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.openxml4j.exceptions.InvalidOperationException; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.struts.action.Action; -import org.apache.struts.action.ActionForm; -import org.apache.struts.action.ActionForward; -import org.apache.struts.action.ActionMapping; -import org.dgfoundation.amp.onepager.util.SessionUtil; -import org.digijava.kernel.ampapi.endpoints.activity.ActivityImportRules; -import org.digijava.kernel.ampapi.endpoints.activity.ActivityInterchangeUtils; -import org.digijava.kernel.ampapi.endpoints.activity.dto.ActivitySummary; -import org.digijava.kernel.ampapi.endpoints.common.JsonApiResponse; -import org.digijava.kernel.persistence.PersistenceManager; -import org.digijava.module.admin.dbentity.ImportStatus; -import org.digijava.module.admin.dbentity.ImportedFilesRecord; -import org.digijava.module.admin.dbentity.ImportedProject; -import org.digijava.module.admin.util.ImportedFileUtil; -import org.digijava.module.admin.util.model.*; -import org.digijava.module.aim.dbentity.*; -import org.digijava.module.aim.form.DataImporterForm; -import org.digijava.module.aim.util.TeamMemberUtil; -import org.digijava.module.categorymanager.dbentity.AmpCategoryValue; -import org.digijava.module.categorymanager.util.CategoryConstants; -import org.hibernate.Query; -import org.hibernate.Session; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.*; -import java.nio.file.Files; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.time.*; -import java.time.format.DateTimeFormatter; -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import static com.fasterxml.jackson.core.JsonGenerator.Feature.ESCAPE_NON_ASCII; - -public class DataImporter extends Action { - static Logger logger = LoggerFactory.getLogger(DataImporter.class); - private static final int BATCH_SIZE = 1000; - private static Map constantsMap=new HashMap<>(); - - - @Override - public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { - // List of fields - List fieldsInfo = getEntityFieldsInfo(); - request.setAttribute("fieldsInfo", fieldsInfo); - DataImporterForm dataImporterForm = (DataImporterForm)form; - - - if (Objects.equals(request.getParameter("action"), "uploadTemplate")) { - logger.info(" this is the action "+request.getParameter("action")); - - InputStream fileInputStream = dataImporterForm.getTemplateFile().getInputStream(); - Workbook workbook = new XSSFWorkbook(fileInputStream); - int numberOfSheets = workbook.getNumberOfSheets(); - Set headersSet = new HashSet<>(); - for (int i=0;i cellIterator = headerRow.cellIterator(); - while (cellIterator.hasNext()) { - Cell cell = cellIterator.next(); - headersSet.add(cell.getStringCellValue()); - } - - } - headersSet = headersSet.stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new)); - StringBuilder headers = new StringBuilder(); - headers.append(" \n"); - response.setHeader("selectTag",headers.toString()); - - - dataImporterForm.getColumnPairs().clear(); - - workbook.close(); - return null; - } - - - - - if (Objects.equals(request.getParameter("action"), "addField")) { - logger.info(" this is the action "+request.getParameter("action")); - - String columnName = request.getParameter("columnName"); - String selectedField = request.getParameter("selectedField"); - dataImporterForm.getColumnPairs().put(columnName, selectedField); - logger.info("Column Pairs:"+dataImporterForm.getColumnPairs()); - - ObjectMapper objectMapper = new ObjectMapper(); - String json = objectMapper.writeValueAsString( dataImporterForm.getColumnPairs()); - - // Send JSON response - response.setContentType("application/json"); - response.getWriter().write(json); - response.setCharacterEncoding("UTF-8"); - - return null; - - } - - - if (Objects.equals(request.getParameter("action"), "removeField")) { - logger.info(" this is the action "+request.getParameter("action")); - - String columnName = request.getParameter("columnName"); - String selectedField = request.getParameter("selectedField"); - dataImporterForm.getColumnPairs().put(columnName, selectedField); - removeMapItem(dataImporterForm.getColumnPairs(),columnName,selectedField); - logger.info("Column Pairs:"+dataImporterForm.getColumnPairs()); - - ObjectMapper objectMapper = new ObjectMapper(); - String json = objectMapper.writeValueAsString( dataImporterForm.getColumnPairs()); - - // Send JSON response - response.setContentType("application/json"); - response.setCharacterEncoding("UTF-8"); -// response.setHeader("updatedMap",json); - response.getWriter().write(json); - - return null; - - } - - if (Objects.equals(request.getParameter("action"), "uploadDataFile")) { - logger.info("This is the action Upload " + request.getParameter("uploadDataFile")); - String fileName = dataImporterForm.getDataFile().getFileName(); - String tempDirPath = System.getProperty("java.io.tmpdir"); - File tempDir = new File(tempDirPath); - if (!tempDir.exists()) { - tempDir.mkdirs(); - } - String tempFilePath = tempDirPath + File.separator + fileName; - try (InputStream inputStream = dataImporterForm.getDataFile().getInputStream(); - FileOutputStream outputStream = new FileOutputStream(tempFilePath)) { - byte[] buffer = new byte[8192]; - int bytesRead; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); - } - } - - // Check if the file is readable and has correct content - File tempFile = new File(tempFilePath); - List similarFiles = ImportedFileUtil.getSimilarFiles(tempFile); - if (similarFiles!=null && !similarFiles.isEmpty()) { - for (ImportedFilesRecord similarFilesRecord : similarFiles) { - logger.info("Similar file: " + similarFilesRecord); - if (similarFilesRecord.getImportStatus().equals(ImportStatus.IN_PROGRESS)) - { - response.setHeader("errorMessage","You have a similar file in progress. Please try again later."); - response.setStatus(400); - return mapping.findForward("importData"); - } - } - } - if (dataImporterForm.getColumnPairs().isEmpty() || !dataImporterForm.getColumnPairs().containsValue("{projectTitle}")) - { - response.setHeader("errorMessage","You must have atleast the {projectTitle} key in your config."); - response.setStatus(400); - return mapping.findForward("importData"); - } - if (!isFileReadable(tempFile) || !isFileContentValid(tempFile)) { - // Handle invalid file - logger.error("Invalid file or content."); - response.setHeader("errorMessage","Unable to parse the file. Please check the file format/content and try again."); - response.setStatus(400); - return mapping.findForward("importData"); - - // Optionally, you can respond with an error message to the client. - } else { - // Proceed with processing the file - ImportedFilesRecord importedFilesRecord = ImportedFileUtil.saveFile(tempFile, fileName); - // Process the file in batches - int res = processFileInBatches(importedFilesRecord, tempFile, request, dataImporterForm.getColumnPairs()); - if (res != 1) { - // Handle error - logger.info("Error processing file " + tempFile); - response.setHeader("errorMessage","Unable to parse the file. Please check the file format/content and try again."); - response.setStatus(400); - return mapping.findForward("importData"); - } - - // Clean up - Files.delete(tempFile.toPath()); - logger.info("Cache map size: " + constantsMap.size()); - constantsMap.clear(); - logger.info("File path is " + tempFilePath + " and size is " + tempFile.length() / (1024 * 1024) + " mb"); - Instant start = Instant.now(); - logger.info("Start time: " + start); - Instant finish = Instant.now(); - long timeElapsed = Duration.between(start, finish).toMillis(); - logger.info("Time Elapsed: " + timeElapsed); - - // Send response - response.setHeader("updatedMap", ""); - dataImporterForm.getColumnPairs().clear(); - } - return null; - } - - return mapping.findForward("importData"); - } - - private void removeMapItem(Map map,String columnName, String selectedField) - { - // Check if the entry's key and value match the criteria - // Remove the entry - map.entrySet().removeIf(entry -> columnName.equals(entry.getKey()) && selectedField.equals(entry.getValue())); - } - - - public static boolean isFileReadable(File file) { - if (file == null || !file.exists() || !file.isFile()) { - return false; - } - return file.canRead(); - } - - // Check if the file content is valid - public static boolean isFileContentValid(File file) { - // Define your validation criteria here - // For example, let's say we want to check if the file contains at least one line - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - String line = reader.readLine(); - return line != null; // If at least one line exists, consider the content valid - } catch (IOException e) { - e.printStackTrace(); // Handle the exception appropriately - return false; // Consider the content invalid if an exception occurs - } - } - - public int processFileInBatches(ImportedFilesRecord importedFilesRecord,File file, HttpServletRequest request,Map config) { - // Open the workbook - int res=0; - ImportedFileUtil.updateFileStatus(importedFilesRecord, ImportStatus.IN_PROGRESS); - try (Workbook workbook = new XSSFWorkbook(file)) { - int numberOfSheets = workbook.getNumberOfSheets(); - logger.info("Number of sheets: " + numberOfSheets); - - // Process each sheet in the workbook - for (int i = 0; i < numberOfSheets; i++) { - logger.info("Sheet number: " + i); - Sheet sheet = workbook.getSheetAt(i); - processSheetInBatches(sheet, request,config, importedFilesRecord); - } - - logger.info("Closing the workbook..."); - ImportedFileUtil.updateFileStatus(importedFilesRecord, ImportStatus.SUCCESS); - res =1; - } catch (IOException e) { - ImportedFileUtil.updateFileStatus(importedFilesRecord, ImportStatus.FAILED); - logger.error("Error processing Excel file: " + e.getMessage(), e); - } catch (InvalidFormatException | InvalidOperationException e) { - logger.error("Error processing Excel file: " + e.getMessage(),e); - } - logger.info("Finished processing file record id: "+importedFilesRecord.getId()+" with status: "+importedFilesRecord.getImportStatus()); - return res; - - } - - private void processSheetInBatches(Sheet sheet, HttpServletRequest request,Map config, ImportedFilesRecord importedFilesRecord) throws JsonProcessingException { - // Get the number of rows in the sheet - int rowCount = sheet.getPhysicalNumberOfRows(); - logger.info("Total number of rows: " + rowCount); - - // Process each row in batches - for (int i = 0; i < rowCount; i += BATCH_SIZE) { - int endIndex = Math.min(i + BATCH_SIZE, rowCount); - List batch = new ArrayList<>(); - - // Retrieve a batch of rows - for (int j = i; j < endIndex; j++) { - Row row = sheet.getRow(j); - if (row != null) { - if (row.getRowNum() == 0) { - continue; - } - batch.add(row); - } - } - - // Process the batch - processBatch(batch, sheet, request,config, importedFilesRecord); - } - } - - private void processBatch(List batch,Sheet sheet, HttpServletRequest request, Map config, ImportedFilesRecord importedFilesRecord) throws JsonProcessingException { - // Process the batch of rows - SessionUtil.extendSessionIfNeeded(request); - Session session = PersistenceManager.getRequestDBSession(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); - for (Row row : batch) { - ImportedProject importedProject= new ImportedProject(); - importedProject.setImportedFilesRecord(importedFilesRecord); - - ImportDataModel importDataModel = new ImportDataModel(); - importDataModel.setModified_by(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeamMemId()); - importDataModel.setCreated_by(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeamMemId()); - importDataModel.setTeam(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeam().getAmpTeamId()); - importDataModel.setIs_draft(true); - OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC); - importDataModel.setCreation_date(now.format(formatter)); - setStatus(importDataModel, session); - - logger.info("Row Number: "+row.getRowNum()+", Sheet Name: "+sheet.getSheetName()); - - for (Map.Entry entry : config.entrySet()) { - int columnIndex = getColumnIndexByName(sheet, entry.getKey()); - if (columnIndex >= 0) { - Cell cell = row.getCell(columnIndex); - switch (entry.getValue()) { - case "{projectTitle}": - importDataModel.setProject_title(cell.getStringCellValue().trim()); - break; - case "{projectDescription}": - importDataModel.setDescription(cell.getStringCellValue().trim()); - break; - case "{projectLocation}": -// ampActivityVersion.addLocation(new AmpActivityLocation()); - break; - case "{primarySector}": - updateSectors(importDataModel, cell.getStringCellValue().trim(), session, true); - break; - case "{secondarySector}": - updateSectors(importDataModel, cell.getStringCellValue().trim(), session, false); - break; - case "{donorAgency}": - updateOrgs(importDataModel, cell.getStringCellValue().trim(), session, "donor"); - break; - case "{fundingItem}": - setAFundingItem(sheet, config, row, entry, importDataModel, session, cell,true,true, "Actual"); - break; - case "{plannedCommitment}": - setAFundingItem(sheet, config, row, entry, importDataModel, session, cell,true,false, "Planned"); - break; - case "{plannedDisbursement}": - setAFundingItem(sheet, config, row, entry, importDataModel, session, cell,false,true, "Planned"); - break; - case "{actualCommitment}": - setAFundingItem(sheet, config, row, entry, importDataModel, session, cell,true,false, "Actual"); - break; - case "{actualDisbursement}": - setAFundingItem(sheet, config, row, entry, importDataModel, session, cell,false,true, "Actual"); - break; - default: - logger.error("Unexpected value: " + entry.getValue()); - break; - } - - - - } - } - importTheData(importDataModel, session, importedProject); - - } - } - - private void setAFundingItem(Sheet sheet, Map config, Row row, Map.Entry entry, ImportDataModel importDataModel, Session session, Cell cell,boolean commitment, boolean disbursement, String - adjustmentType) { - int detailColumn = getColumnIndexByName(sheet, getKey(config, "{financingInstrument}")); - String finInstrument= detailColumn>=0? row.getCell(detailColumn).getStringCellValue(): ""; - detailColumn = getColumnIndexByName(sheet, getKey(config, "{typeOfAssistance}")); - String typeOfAss = detailColumn>=0? row.getCell(detailColumn).getStringCellValue(): ""; - - if (importDataModel.getDonor_organization()==null || importDataModel.getDonor_organization().isEmpty()) - { - if (!config.containsValue("{donorAgency}")) - { - updateFunding(importDataModel, session, cell.getNumericCellValue(), entry.getKey(), getRandomOrg(session),typeOfAss,finInstrument, commitment,disbursement, adjustmentType); - - } - else { - int columnIndex1 = getColumnIndexByName(sheet, getKey(config, "{donorAgency}")); - updateOrgs(importDataModel, columnIndex1>=0? row.getCell(columnIndex1).getStringCellValue().trim():"no org", session, "donor"); - updateFunding(importDataModel, session, cell.getNumericCellValue(), entry.getKey(), new ArrayList<>(importDataModel.getDonor_organization()).get(0).getOrganization(),typeOfAss,finInstrument,commitment,disbursement, adjustmentType); - } - - }else { - updateFunding(importDataModel, session, cell.getNumericCellValue(), entry.getKey(), new ArrayList<>(importDataModel.getDonor_organization()).get(0).getOrganization(),typeOfAss,finInstrument,commitment,disbursement, adjustmentType); - } - } - - public K getKey(Map map, V value) { - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue().equals(value)) { - return entry.getKey(); - } - } - return null; - } - - public static String findYearSubstring(String text) { - Pattern pattern = Pattern.compile("(?:19|20)\\d{2}"); - Matcher matcher = pattern.matcher(text); - if (matcher.find()) { - return matcher.group(); - } else { - return null; - } - } - - - private String getFundingDate(String yearString) - { - LocalDate date = LocalDate.of(Integer.parseInt(yearString), 1, 1); - - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - - return date.format(formatter); - } - private void updateFunding(ImportDataModel importDataModel, Session session, Number amount, String columnHeader, Long orgId, String assistanceType, String finInst, boolean commitment, boolean disbursement, String - adjustmentType) { - String catHql="SELECT s FROM " + AmpCategoryValue.class.getName() + " s JOIN s.ampCategoryClass c WHERE c.keyName = :categoryKey"; - Long currencyId = getCurrencyId(session); - Long adjType = getCategoryValue(session, "adjustmentType", CategoryConstants.ADJUSTMENT_TYPE_KEY,catHql,adjustmentType ); - Long assType = getCategoryValue(session, "assistanceType", CategoryConstants.TYPE_OF_ASSISTENCE_KEY, catHql,assistanceType); - Long finInstrument = getCategoryValue(session, "finInstrument", CategoryConstants.FINANCING_INSTRUMENT_KEY, catHql,finInst); - Long orgRole = getOrganizationRole(session); - - String yearString = findYearSubstring(columnHeader); - String fundingDate = yearString != null ? getFundingDate(yearString) : getFundingDate("2000"); - - Funding funding = new Funding(); - funding.setDonor_organization_id(orgId); - funding.setType_of_assistance(assType); - funding.setFinancing_instrument(finInstrument); - funding.setSource_role(orgRole); - - Transaction transaction = new Transaction(); - transaction.setCurrency(currencyId); - transaction.setAdjustment_type(adjType); - transaction.setTransaction_amount(amount.doubleValue()); - transaction.setTransaction_date(fundingDate); - if (commitment) { - funding.getCommitments().add(transaction); - } - if (disbursement) { - funding.getDisbursements().add(transaction); - } - - DonorOrganization donorOrganization = new DonorOrganization(); - donorOrganization.setOrganization(orgId); - donorOrganization.setPercentage(100.0); - - importDataModel.getDonor_organization().add(donorOrganization); - importDataModel.getFundings().add(funding); - } - - - private Long getOrganizationRole(Session session) { - - if (constantsMap.containsKey("orgRole")) { - Long val= constantsMap.get("orgRole"); - logger.info("In cache... orgRole: "+val); - return val; - - } - if (!session.isOpen()) { - session = PersistenceManager.getRequestDBSession(); - } - String hql = "SELECT o.ampRoleId FROM " + AmpRole.class.getName() + " o WHERE LOWER(o.name) LIKE LOWER(:name)"; - - Query query = session.createQuery(hql); - query.setParameter("name", "%donor%"); - List orgRoles = query.list(); - Long orgRole = orgRoles.get(0); - constantsMap.put("orgRole", orgRole); - return orgRole; - } - - private Long getCurrencyId(Session session) { - - if (constantsMap.containsKey("currencyId")) { - Long val = constantsMap.get("currencyId"); - logger.info("In cache... currency: "+val); - return val; - - } - if (!session.isOpen()) { - session = PersistenceManager.getRequestDBSession(); - } - String hql = "SELECT ac.ampCurrencyId FROM " + AmpCurrency.class.getName() + " ac " + - "WHERE ac.currencyCode = :currencyCode"; - - Query query = session.createQuery(hql); - query.setString("currencyCode", "XOF"); - Long currencyId = (Long) query.uniqueResult(); - constantsMap.put("currencyId", currencyId); - return currencyId; - } - - private Long getCategoryValue(Session session, String constantKey, String categoryKey, String hql, String possibleValue) { - String fullKey=constantKey+"_"+possibleValue; - if (constantsMap.containsKey(fullKey)) { - Long val = constantsMap.get(fullKey); - logger.info("In cache... "+fullKey+":"+val); - return val; - - } - if (!session.isOpen()) { - session = PersistenceManager.getRequestDBSession(); - } - Query query = session.createQuery(hql); - query.setParameter("categoryKey", categoryKey); - List values = query.list(); - Long categoryId = ((AmpCategoryValue) values.get(0)).getId(); - - if (!Objects.equals(possibleValue, "") && !Objects.equals(possibleValue, null)) - { - for (Object categoryValue : values) - { - if (Objects.equals(((AmpCategoryValue) categoryValue).getValue().toLowerCase(), possibleValue.toLowerCase())) - { - categoryId = ((AmpCategoryValue) categoryValue).getId(); - logger.info("Found category: "+((AmpCategoryValue) categoryValue).getValue()); - break; - } - - } - } - logger.info("Found category: "+categoryId +" for "+constantKey+"_"+possibleValue); - constantsMap.put(fullKey, categoryId); - return categoryId; - } - - private AmpActivityVersion existingActivity(ImportDataModel importDataModel,Session session) - { - if (!session.isOpen()) { - session=PersistenceManager.getRequestDBSession(); - } - String hql = "SELECT a FROM " + AmpActivityVersion.class.getName() + " a " + - "WHERE a.name = :name"; - Query query= session.createQuery(hql); - query.setString("name", importDataModel.getProject_title()); - List ampActivityVersions=query.list(); - return !ampActivityVersions.isEmpty()? ampActivityVersions.get(ampActivityVersions.size()-1) :null; - } - private void setStatus(ImportDataModel importDataModel,Session session) - { - String hql = "SELECT s FROM " + AmpCategoryValue.class.getName() + " s " + - "JOIN s.ampCategoryClass c " + - "WHERE c.keyName = :categoryKey"; - Long statusId = getCategoryValue(session,"statusId",CategoryConstants.ACTIVITY_STATUS_KEY,hql,""); - importDataModel.setActivity_status(statusId); - - } - private void importTheData(ImportDataModel importDataModel, Session session, ImportedProject importedProject) throws JsonProcessingException { - if (!session.isOpen()) { - session=PersistenceManager.getRequestDBSession(); - } - ActivityImportRules rules = new ActivityImportRules(true, false, - true); - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.configure(ESCAPE_NON_ASCII, false); // Disable escaping of non-ASCII characters during serialization - objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); - - Map map = objectMapper - .convertValue(importDataModel, new TypeReference>() {}); - JsonApiResponse response; - AmpActivityVersion existing = existingActivity(importDataModel,session); - logger.info("Existing ?"+existing); - logger.info("Data model object: "+importDataModel); - if (existing==null){ - logger.info("New activity"); - importedProject.setNewProject(true); - response= ActivityInterchangeUtils.importActivity(map, false, rules, "activity/new"); - } - else - { - logger.info("Existing activity"); - importedProject.setNewProject(false); - importDataModel.setInternal_id(existing.getAmpActivityId()); - importDataModel.setAmp_id(existing.getAmpId()); - ActivityGroup activityGroup= new ActivityGroup(); - activityGroup.setVersion(existing.getAmpActivityGroup().getVersion()); - importDataModel.setActivity_group(activityGroup); - map = objectMapper - .convertValue(importDataModel, new TypeReference>() {}); - response= ActivityInterchangeUtils.importActivity(map, true, rules, "activity/update"); - - } - if (response!=null) { - if (!response.getErrors().isEmpty()) { - importedProject.setImportStatus(ImportStatus.FAILED); - } else { - importedProject.setImportStatus(ImportStatus.SUCCESS); - - } - } - - String resp = objectMapper.writeValueAsString(response); - importedProject.setImportResponse(resp); - if (!session.isOpen()) { - session=PersistenceManager.getRequestDBSession(); - } - session.saveOrUpdate(importedProject); - logger.info("Imported project: "+importedProject); - } - - - - private void updateSectors(ImportDataModel importDataModel, String name, Session session, boolean primary) - { - - if (constantsMap.containsKey("sector_"+name)) { - Long sectorId = constantsMap.get("sector_"+name); - logger.info("In cache... sector "+"sector_"+name+":"+sectorId); - createSector(importDataModel,primary,sectorId); - } - else { - if (!session.isOpen()) { - session = PersistenceManager.getRequestDBSession(); - } - - session.doWork(connection -> { - String query = primary ? "SELECT ams.amp_sector_id AS amp_sector_id, ams.name AS name FROM amp_sector ams JOIN amp_classification_config acc ON ams.amp_sec_scheme_id=acc.classification_id WHERE LOWER(ams.name) = LOWER(?) AND acc.name='Primary'" : "SELECT ams.amp_sector_id AS amp_sector_id, ams.name AS name FROM amp_sector ams JOIN amp_classification_config acc ON ams.amp_sec_scheme_id=acc.classification_id WHERE LOWER(ams.name) = LOWER(?) AND acc.name='Secondary'"; - try (PreparedStatement statement = connection.prepareStatement(query)) { - // Set the name as a parameter to the prepared statement - statement.setString(1, name); - - // Execute the query and process the results - try (ResultSet resultSet = statement.executeQuery()) { - while (resultSet.next()) { - Long ampSectorId = resultSet.getLong("amp_sector_id"); - createSector(importDataModel, primary, ampSectorId); - constantsMap.put("sector_"+name, ampSectorId); - } - } - - } catch (SQLException e) { - logger.error("Error getting sectors", e); - } - }); - } - - - - } - - private static void createSector(ImportDataModel importDataModel, boolean primary, Long ampSectorId) { - Sector sector1 = new Sector(); - sector1.setSector_percentage(100.00); - sector1.setSector(ampSectorId); - if (primary) { - importDataModel.getPrimary_sectors().add(sector1); - } - else - { - importDataModel.getSecondary_sectors().add(sector1); - - } - } - - private Long getRandomOrg(Session session) - { - Long randomOrg; - if (constantsMap.containsKey("randomOrg")) { - randomOrg = constantsMap.get("randomOrg"); - logger.info("In cache... randomOrg "+randomOrg); - }else { - if (!session.isOpen()) { - session = PersistenceManager.getRequestDBSession(); - } - String hql = "SELECT o.ampOrgId FROM " + AmpOrganisation.class.getName() + " o"; - - randomOrg = (Long) session.createQuery(hql).setMaxResults(1).uniqueResult(); - constantsMap.put("randomOrg",randomOrg); - } - return randomOrg; - - - } - - private void updateOrgs(ImportDataModel importDataModel, String name, Session session, String type) - { - Long orgId; - - if (constantsMap.containsKey("org_"+name)) { - orgId = constantsMap.get("org_"+name); - logger.info("In cache... organisation "+"org_"+name+":"+orgId); - } - else { - if (!session.isOpen()) { - session = PersistenceManager.getRequestDBSession(); - } - String hql = "SELECT o.ampOrgId FROM " + AmpOrganisation.class.getName() + " o WHERE LOWER(o.name) LIKE LOWER(:name)"; - - Query query = session.createQuery(hql); - query.setParameter("name", "%" + name + "%"); - List organisations = query.list(); - if (!organisations.isEmpty()) { - orgId = organisations.get(0); - } else { - hql = "SELECT o.ampOrgId FROM " + AmpOrganisation.class.getName() + " o"; - - query = session.createQuery(hql).setMaxResults(1); - orgId = (Long) query.uniqueResult(); - } - constantsMap.put("org_" + name, orgId); - } - logger.info("Organisation: " + orgId); - - if (Objects.equals(type, "donor")) { - DonorOrganization donorOrganization = new DonorOrganization(); - donorOrganization.setOrganization(orgId); - donorOrganization.setPercentage(100.0); - importDataModel.getDonor_organization().add(donorOrganization); - } - - - - } - - - private static int getColumnIndexByName(Sheet sheet, String columnName) { - try { - Row headerRow = sheet.getRow(0); - for (int i = 0; i < headerRow.getLastCellNum(); i++) { - Cell cell = headerRow.getCell(i); - if (cell != null && columnName.equals(cell.getStringCellValue())) { - return i; - } - } - return -1; - }catch (Exception e) - { - logger.error("Error getting column index for "+columnName,e); - return -1; - } - - } - private List getEntityFieldsInfo() { - List fieldsInfos = new ArrayList<>(); - fieldsInfos.add("{projectTitle}"); - fieldsInfos.add("{projectDescription}"); - fieldsInfos.add("{primarySector}"); - fieldsInfos.add("{secondarySector}"); - fieldsInfos.add("{projectLocation}"); - fieldsInfos.add("{projectStartDate}"); - fieldsInfos.add("{projectEndDate}"); - fieldsInfos.add("{donorAgency}"); - fieldsInfos.add("{executingAgency}"); - fieldsInfos.add("{implementingAgency}"); - fieldsInfos.add("{actualDisbursement}"); - fieldsInfos.add("{actualCommitment}"); - fieldsInfos.add("{plannedDisbursement}"); - fieldsInfos.add("{plannedCommitment}"); - fieldsInfos.add("{fundingItem}"); - fieldsInfos.add("{financingInstrument}"); - fieldsInfos.add("{typeOfAssistance}"); - fieldsInfos.add("{secondarySubSector}"); - return fieldsInfos.stream().sorted().collect(Collectors.toList()); - } - -} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/UpdateComponents.java b/amp/WEB-INF/src/org/digijava/module/aim/action/UpdateComponents.java index 21cb01fe9c7..49d5856c59b 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/action/UpdateComponents.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/UpdateComponents.java @@ -34,8 +34,8 @@ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServlet return mapping.findForward("index"); } } - - + + // logger.debug("\n\ncame into the update components manager"); String event = request.getParameter("event"); String compId = request.getParameter("componentId"); @@ -52,9 +52,8 @@ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServlet updCompForm.setCompType(null); } else if (event.equalsIgnoreCase("edit")) { - Iterator itr = ComponentsUtil.getComponentForEditing(new Long(compId)).iterator(); - while (itr.hasNext()) { - AmpComponent ampComp = (AmpComponent) itr.next(); + for (Object o : ComponentsUtil.getComponentForEditing(new Long(compId))) { + AmpComponent ampComp = (AmpComponent) o; updCompForm.setId(ampComp.getAmpComponentId()); updCompForm.setCompTitle(ampComp.getTitle()); updCompForm.setCompDes(ampComp.getDescription()); diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/ViewImportProgress.java b/amp/WEB-INF/src/org/digijava/module/aim/action/ViewImportProgress.java index b560ce4913b..8c2ae7cbc17 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/action/ViewImportProgress.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/ViewImportProgress.java @@ -7,9 +7,9 @@ import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.digijava.kernel.persistence.PersistenceManager; -import org.digijava.module.admin.dbentity.ImportStatus; -import org.digijava.module.admin.dbentity.ImportedFilesRecord; -import org.digijava.module.admin.dbentity.ImportedProject; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportStatus; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedFilesRecord; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedProject; import org.digijava.module.aim.form.ImportProgressForm; import org.hibernate.Query; import org.hibernate.Session; diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/DataImporter.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/DataImporter.java new file mode 100644 index 00000000000..e3fbff3dfbe --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/DataImporter.java @@ -0,0 +1,403 @@ +package org.digijava.module.aim.action.dataimporter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.opencsv.CSVParser; +import com.opencsv.CSVParserBuilder; +import com.opencsv.CSVReader; +import com.opencsv.CSVReaderBuilder; +import com.opencsv.exceptions.CsvValidationException; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.digijava.kernel.persistence.PersistenceManager; +import org.digijava.module.aim.action.dataimporter.dbentity.DataImporterConfig; +import org.digijava.module.aim.action.dataimporter.dbentity.DataImporterConfigValues; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportStatus; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedFilesRecord; +import org.digijava.module.aim.action.dataimporter.util.ImportedFileUtil; +import org.digijava.module.aim.form.DataImporterForm; +import org.digijava.module.aim.util.FeaturesUtil; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.type.StringType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.nio.file.Files; +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDateTime; +import java.util.*; +import java.util.stream.Collectors; + +import static org.digijava.module.aim.action.dataimporter.ExcelImporter.processExcelFileInBatches; +import static org.digijava.module.aim.action.dataimporter.util.ImporterUtil.*; + +public class DataImporter extends Action { + static Logger logger = LoggerFactory.getLogger(DataImporter.class); + + @Override + public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { + // List of fields + List fieldsInfo = getEntityFieldsInfo(); + request.setAttribute("fieldsInfo", fieldsInfo); + List configNames= getConfigNames(); + request.setAttribute("configNames", configNames); + DataImporterForm dataImporterForm = (DataImporterForm) form; + + if (Objects.equals(request.getParameter("action"), "configByName")) { + logger.info(" this is the action " + request.getParameter("action")); + String configName = request.getParameter("configName"); + Map config= getConfigByName(configName); + dataImporterForm.setColumnPairs(config); + + logger.info("Column Pairs:" + config); + + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(config); + + // Send response + response.setContentType("application/json"); + response.getWriter().write(json); + response.setCharacterEncoding("UTF-8"); + + return null; + } + + + if (Objects.equals(request.getParameter("action"), "uploadTemplate")) { + logger.info(" this is the action " + request.getParameter("action")); + if (request.getParameter("uploadTemplate") != null) { + logger.info(" this is the action " + request.getParameter("uploadTemplate")); + Set headersSet = new HashSet<>(); + + if (request.getParameter("fileType") != null) { + InputStream fileInputStream = dataImporterForm.getTemplateFile().getInputStream(); + if ((Objects.equals(request.getParameter("fileType"), "excel") || Objects.equals(request.getParameter("fileType"), "csv"))) { + Workbook workbook = new XSSFWorkbook(fileInputStream); + int numberOfSheets = workbook.getNumberOfSheets(); + for (int i = 0; i < numberOfSheets; i++) { + Sheet sheet = workbook.getSheetAt(i); + Row headerRow = sheet.getRow(0); + Iterator cellIterator = headerRow.cellIterator(); + while (cellIterator.hasNext()) { + Cell cell = cellIterator.next(); + headersSet.add(cell.getStringCellValue()); + } + + } + workbook.close(); + + + } else if (Objects.equals(request.getParameter("fileType"), "text")) { + CSVParser parser = new CSVParserBuilder().withSeparator(request.getParameter("dataSeparator").charAt(0)).build(); + + try (CSVReader reader = new CSVReaderBuilder(new InputStreamReader(fileInputStream)).withCSVParser(parser).build()) { + String[] headers = reader.readNext(); // Read the first line which contains headers + + if (headers != null) { + // Print each header + headersSet.addAll(Arrays.asList(headers)); + } else { + logger.info("File is empty or does not contain headers."); + } + } catch (IOException | CsvValidationException e) { + logger.error("An error occurred during extraction of headers.",e); + } + + } + } + headersSet = headersSet.stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new)); + StringBuilder headers = new StringBuilder(); + headers.append(" \n"); + response.setHeader("selectTag", headers.toString()); + + response.setHeader("updatedMap", ""); + + dataImporterForm.getColumnPairs().clear(); + + } + return null; + } + + + if (Objects.equals(request.getParameter("action"), "addField")) { + logger.info(" this is the action " + request.getParameter("action")); + + String columnName = request.getParameter("columnName"); + String selectedField = request.getParameter("selectedField"); + dataImporterForm.getColumnPairs().put(columnName, selectedField); + logger.info("Column Pairs:" + dataImporterForm.getColumnPairs()); + + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(dataImporterForm.getColumnPairs()); + + // Send response + response.setContentType("application/json"); + response.getWriter().write(json); + response.setCharacterEncoding("UTF-8"); + + return null; + + } + + + if (Objects.equals(request.getParameter("action"), "removeField")) { + logger.info(" this is the action " + request.getParameter("action")); + + String columnName = request.getParameter("columnName"); + String selectedField = request.getParameter("selectedField"); + dataImporterForm.getColumnPairs().put(columnName, selectedField); + removeMapItem(dataImporterForm.getColumnPairs(), columnName, selectedField); + logger.info("Column Pairs:" + dataImporterForm.getColumnPairs()); + + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(dataImporterForm.getColumnPairs()); + + // Send response + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(json); + + return null; + + } + + if (Objects.equals(request.getParameter("action"), "uploadDataFile")) { + logger.info("This is the action " + request.getParameter("action")); + String fileName = dataImporterForm.getDataFile().getFileName(); + String tempDirPath = System.getProperty("java.io.tmpdir"); + File tempDir = new File(tempDirPath); + if (!tempDir.exists()) { + tempDir.mkdirs(); + } + String tempFilePath = tempDirPath + File.separator + fileName; + try (InputStream inputStream = dataImporterForm.getDataFile().getInputStream(); + FileOutputStream outputStream = new FileOutputStream(tempFilePath)) { + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } + + // Check if the file is readable and has correct content + File tempFile = new File(tempFilePath); + List similarFiles = ImportedFileUtil.getSimilarFiles(tempFile); + if (similarFiles != null && !similarFiles.isEmpty()) { + for (ImportedFilesRecord similarFilesRecord : similarFiles) { + logger.info("Similar file: " + similarFilesRecord); + if (similarFilesRecord.getImportStatus().equals(ImportStatus.IN_PROGRESS)) { + response.setHeader("errorMessage", "You have a similar file in progress. Please try again later."); + response.setStatus(400); + return mapping.findForward("importData"); + } + } + } + if (dataImporterForm.getColumnPairs().isEmpty() ||(!dataImporterForm.getColumnPairs().containsValue("Project Title") && !dataImporterForm.getColumnPairs().containsValue("Project Code"))) { + + response.setHeader("errorMessage", "You must have at least the 'Project Title' or 'Project Code' column in your config."); + response.setStatus(400); + return mapping.findForward("importData"); + } + if (!isFileReadable(tempFile) || !isFileContentValid(tempFile)) { + // Handle invalid file + logger.error("Invalid file or content."); + response.setHeader("errorMessage", "Unable to parse the file. Please check the file format/content and try again."); + response.setStatus(400); + return mapping.findForward("importData"); + + } else { + // Proceed with processing the file + String existingConfig = request.getParameter("existingConfig"); + logger.info("Existing configuration: {}",existingConfig); + if (!Objects.equals(existingConfig, "1")) { + saveImportConfig(request, fileName, dataImporterForm.getColumnPairs()); + } + + int res = 0; + ImportedFilesRecord importedFilesRecord = ImportedFileUtil.saveFile(tempFile, fileName); + logger.info("Saved file record: {}",importedFilesRecord); + boolean isInternal= dataImporterForm.isInternal(); + logger.info("Internal: "+ isInternal); + if (isInternal) + { + dataImporterForm.getColumnPairs().put("Donor Agency", "Donor Agency"); + } + logger.info("Configuration"+ dataImporterForm.getColumnPairs()); + if ((Objects.equals(request.getParameter("fileType"), "excel") || Objects.equals(request.getParameter("fileType"), "csv"))) { + + // Process the file in batches + res = processExcelFileInBatches(importedFilesRecord, tempFile, request, dataImporterForm.getColumnPairs(), isInternal); + } else if ( Objects.equals(request.getParameter("fileType"), "text")) { + res=TxtDataImporter.processTxtFileInBatches(importedFilesRecord, tempFile, request, dataImporterForm.getColumnPairs(), isInternal); + } + if (res != 1) { + // Handle error + logger.info("Error processing file " + tempFile); + response.setHeader("errorMessage", "Unable to parse the file. Please check the file format/content and try again."); + response.setStatus(400); + return mapping.findForward("importData"); + } + + + // Clean up + ImportedFileUtil.updateFileStatus(importedFilesRecord, ImportStatus.SUCCESS); + Files.delete(tempFile.toPath()); + logger.info("Cache map size: " + ConstantsMap.size()); + ConstantsMap.clear(); + logger.info("File path is " + tempFilePath + " and size is " + tempFile.length() / (1024 * 1024) + " mb"); + Instant start = Instant.now(); + logger.info("Start time: " + start); + Instant finish = Instant.now(); + long timeElapsed = Duration.between(start, finish).toMillis(); + logger.info("Time Elapsed: " + timeElapsed); + + // Send response + response.setHeader("updatedMap", ""); + dataImporterForm.getColumnPairs().clear(); + } + return null; + } + + return mapping.findForward("importData"); + } + private static List getConfigNames() + { + Session session = PersistenceManager.getRequestDBSession(); + + if (!session.isOpen()) { + session=PersistenceManager.getRequestDBSession(); + } + String hql = "SELECT c.configName FROM DataImporterConfig c"; + Query query = session.createQuery(hql); + + List< String> configNames = query.list(); + return configNames==null?Collections.emptyList():configNames; + + } + + private static Map getConfigByName(String configName) { + logger.info("Getting import config for configName: {}", configName); + Session session = PersistenceManager.getRequestDBSession(); + Map configValues = new HashMap<>(); + + String hql = "FROM DataImporterConfig WHERE configName = :configName"; + Query query = session.createQuery(hql); + query.setParameter("configName", configName, StringType.INSTANCE); + query.setMaxResults(1); + + List resultList = query.list(); + logger.info("Configs found: {}",resultList); + + + if (!resultList.isEmpty()) { + Set values = resultList.get(0).getConfigValues(); + logger.info("Config Values found: {}",values); + + if (!values.isEmpty()) + { + values.forEach(value-> configValues.put(value.getConfigKey(),value.getConfigValue())); + } + } + + return configValues; + } + private static void saveImportConfig(HttpServletRequest request,String fileName, Map config) + { + logger.info("Saving import config"); + Session session = PersistenceManager.getRequestDBSession(); + + if (!session.isOpen()) { + session=PersistenceManager.getRequestDBSession(); + } + String configName= fileName+"_"+ LocalDateTime.now().toString().replace(":", "_"); + + if (request.getParameter("configName") != null) + { + configName=request.getParameter("configName"); + Query query = session.createQuery("FROM DataImporterConfig WHERE configName = :configName"); + query.setParameter("configName", configName); + List existingConfigs = query.list(); + + if (!existingConfigs.isEmpty()) { + configName += "_" + LocalDateTime.now().toString().replace(":", "_"); + } + } + + DataImporterConfig dataImporterConfig= new DataImporterConfig(); + Set configValues = new HashSet<>(); + dataImporterConfig.setConfigName(configName); + session.saveOrUpdate(dataImporterConfig); + for (Map.Entry entry : config.entrySet()) { + DataImporterConfigValues configValue = new DataImporterConfigValues(); + configValue.setConfigKey(entry.getKey()); + configValue.setConfigValue(entry.getValue()); + configValue.setDataImporterConfig(dataImporterConfig); + configValues.add(configValue); + session.saveOrUpdate(configValue); + } + + + dataImporterConfig.setConfigValues(configValues); + + + + logger.info("Saved configuration: {}", dataImporterConfig); + + } + + + + + + + private List getEntityFieldsInfo() { + List fieldsInfos = new ArrayList<>(); + fieldsInfos.add("Project Title"); + fieldsInfos.add("Project Code"); + fieldsInfos.add("Project Description"); + fieldsInfos.add("Primary Sector"); + fieldsInfos.add("Secondary Sector"); + fieldsInfos.add("Project Location"); + fieldsInfos.add("Project Start Date"); + fieldsInfos.add("Project End Date"); + fieldsInfos.add("Donor Agency"); + fieldsInfos.add("Exchange Rate"); + fieldsInfos.add("Donor Agency Code"); + fieldsInfos.add("Responsible Organization"); + fieldsInfos.add("Responsible Organization Code"); + fieldsInfos.add("Executing Agency"); + fieldsInfos.add("Implementing Agency"); + fieldsInfos.add("Actual Disbursement"); + fieldsInfos.add("Actual Commitment"); + fieldsInfos.add("Actual Expenditure"); + fieldsInfos.add("Planned Disbursement"); + fieldsInfos.add("Planned Commitment"); + fieldsInfos.add("Planned Expenditure"); + fieldsInfos.add("Funding Item"); + fieldsInfos.add("Transaction Date"); + fieldsInfos.add("Financing Instrument"); + fieldsInfos.add("Type Of Assistance"); + fieldsInfos.add("Secondary Subsector"); + fieldsInfos.add("Currency"); + fieldsInfos.add("Component Name"); + fieldsInfos.add("Component Code"); + fieldsInfos.add("Beneficiary Agency"); + return fieldsInfos.stream().sorted().collect(Collectors.toList()); + } + +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/ExcelImporter.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/ExcelImporter.java new file mode 100644 index 00000000000..19c5fed2fa3 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/ExcelImporter.java @@ -0,0 +1,247 @@ +package org.digijava.module.aim.action.dataimporter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.exceptions.InvalidOperationException; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.dgfoundation.amp.onepager.util.SessionUtil; +import org.digijava.kernel.persistence.PersistenceManager; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportStatus; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedFilesRecord; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedProject; +import org.digijava.module.aim.action.dataimporter.model.Funding; +import org.digijava.module.aim.action.dataimporter.model.ImportDataModel; +import org.digijava.module.aim.action.dataimporter.util.ImportedFileUtil; +import org.digijava.module.aim.dbentity.AmpActivityVersion; +import org.digijava.module.aim.util.FeaturesUtil; +import org.digijava.module.aim.util.TeamMemberUtil; +import org.hibernate.Session; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static org.digijava.module.aim.action.dataimporter.util.ImporterUtil.*; + +public class ExcelImporter { + static Logger logger = LoggerFactory.getLogger(ExcelImporter.class); + private static final int BATCH_SIZE = 1000; + + public static int processExcelFileInBatches(ImportedFilesRecord importedFilesRecord, File file, HttpServletRequest request, Map config, boolean isInternal) { + int res=0; + ImportedFileUtil.updateFileStatus(importedFilesRecord, ImportStatus.IN_PROGRESS); + try (Workbook workbook = new XSSFWorkbook(file)) { + int numberOfSheets = workbook.getNumberOfSheets(); + logger.info("Number of sheets: " + numberOfSheets); + + // Process each sheet in the workbook + for (int i = 0; i < numberOfSheets; i++) { + logger.info("Sheet number: " + i); + Sheet sheet = workbook.getSheetAt(i); + if (isInternal) { + addDonorAgencyColumn(sheet, FeaturesUtil.getGlobalSettingValue("Internal Ecowas Donor")); + + } + + processSheetInBatches(sheet, request,config, importedFilesRecord); + } + + logger.info("Closing the workbook..."); + res =1; + } catch (IOException e) { + ImportedFileUtil.updateFileStatus(importedFilesRecord, ImportStatus.FAILED); + logger.error("Error processing Excel file: " + e.getMessage(), e); + } catch (InvalidFormatException | InvalidOperationException e) { + logger.error("Error processing Excel file: " + e.getMessage(),e); + } + logger.info("Finished processing file record id: "+importedFilesRecord.getId()+" with status: "+importedFilesRecord.getImportStatus()); + return res; + + } + + private static void addDonorAgencyColumn(Sheet sheet, String donorAgencyValue) { + // Get the header row, create if it doesn't exist + logger.info("Adding Ecowas column"); + Row headerRow = sheet.getRow(0); + if (headerRow == null) { + headerRow = sheet.createRow(0); + } + + // Set the header for the "Donor Agency" column + int donorAgencyColumnIndex = headerRow.getLastCellNum(); + Cell headerCell = headerRow.createCell(donorAgencyColumnIndex); + headerCell.setCellValue("Donor Agency"); + + // Populate each row in the new column with the donor agency value + for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { + Row row = sheet.getRow(rowIndex); + if (row == null) { + row = sheet.createRow(rowIndex); + } + + Cell cell = row.createCell(donorAgencyColumnIndex); + cell.setCellValue(donorAgencyValue); + logger.info("Cell newly created: " + row.getCell(donorAgencyColumnIndex).getStringCellValue()); + } + } + + + public static void processSheetInBatches(Sheet sheet, HttpServletRequest request,Map config, ImportedFilesRecord importedFilesRecord) throws JsonProcessingException { + // Get the number of rows in the sheet + int rowCount = sheet.getPhysicalNumberOfRows(); + logger.info("There are {} rows in sheet {} " , rowCount, sheet.getSheetName()); + + // Process each row in batches + for (int i = 0; i < rowCount; i += BATCH_SIZE) { + int endIndex = Math.min(i + BATCH_SIZE, rowCount); + List batch = new ArrayList<>(); + + // Retrieve a batch of rows + for (int j = i; j < endIndex; j++) { + Row row = sheet.getRow(j); + if (row != null) { + if (row.getRowNum() == 0) { + continue; + } + batch.add(row); + } + } + + // Process the batch + processBatch(batch, sheet, request,config, importedFilesRecord); + } + } + + + public static void processBatch(List batch,Sheet sheet, HttpServletRequest request, Map config, ImportedFilesRecord importedFilesRecord) throws JsonProcessingException { + // Process the batch of rows + SessionUtil.extendSessionIfNeeded(request); + Session session = PersistenceManager.getRequestDBSession(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + for (Row row : batch) { + if (row != null) { + ImportedProject importedProject = new ImportedProject(); + importedProject.setImportedFilesRecord(importedFilesRecord); + List fundings = new ArrayList<>(); + + ImportDataModel importDataModel = new ImportDataModel(); + importDataModel.setModified_by(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeamMemId()); + importDataModel.setCreated_by(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeamMemId()); + importDataModel.setTeam(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeam().getAmpTeamId()); + importDataModel.setIs_draft(true); + OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC); + importDataModel.setCreation_date(now.format(formatter)); + setStatus(importDataModel); + + int componentCodeColumn = getColumnIndexByName(sheet, getKey(config, "Component Code")); + String componentCode = componentCodeColumn >= 0 ? getStringValueFromCell(row.getCell(componentCodeColumn),true) : null; + + int componentNameColumn = getColumnIndexByName(sheet, getKey(config, "Component Name")); + String componentName = componentNameColumn >= 0 ? getStringValueFromCell(row.getCell(componentNameColumn),true): null; + + int donorAgencyCodeColumn = getColumnIndexByName(sheet, getKey(config, "Donor Agency Code")); + String donorAgencyCode = donorAgencyCodeColumn >= 0 ? getStringValueFromCell(row.getCell(donorAgencyCodeColumn),true) : null; + + int responsibleOrgCodeColumn = getColumnIndexByName(sheet, getKey(config, "Responsible Organization Code")); + String responsibleOrgCode = responsibleOrgCodeColumn >= 0 ? getStringValueFromCell(row.getCell(responsibleOrgCodeColumn),true) : null; + + + int projectCodeColumn = getColumnIndexByName(sheet, getKey(config, "Project Code")); + String projectCode = projectCodeColumn >= 0 ? getStringValueFromCell(row.getCell(projectCodeColumn),false) : ""; + importDataModel.setProject_code(projectCode); + + int projectTitleColumn = getColumnIndexByName(sheet, getKey(config, "Project Title")); + String projectTitle = projectTitleColumn >= 0 ? getStringValueFromCell(row.getCell(projectTitleColumn),false) : ""; + importDataModel.setProject_title(projectTitle); + + int projectDescColumn = getColumnIndexByName(sheet, getKey(config, "Project Description")); + String projectDesc = projectDescColumn >= 0 ? getStringValueFromCell(row.getCell(projectDescColumn),false) : null; + importDataModel.setDescription(projectDesc); + + AmpActivityVersion existing = existingActivity(projectTitle, projectCode, session); + Long responsibleOrgId = null; + + logger.info("Row Number: {}, Sheet Name: {}", row.getRowNum(), sheet.getSheetName()); + for (Map.Entry entry : config.entrySet()) { + Funding fundingItem = new Funding(); + + int columnIndex = getColumnIndexByName(sheet, entry.getKey()); + + if (columnIndex >= 0) { + Cell cell = row.getCell(columnIndex); + switch (entry.getValue()) { + case "Project Location": + updateLocations(importDataModel,Objects.requireNonNull(getStringValueFromCell(cell, false)).trim(),session); + break; + case "Primary Sector": + updateSectors(importDataModel, Objects.requireNonNull(getStringValueFromCell(cell, false)).trim(), session, true); + break; + case "Secondary Sector": + updateSectors(importDataModel, Objects.requireNonNull(getStringValueFromCell(cell, false)).trim(), session, false); + break; + case "Donor Agency": + logger.info("Getting donor"); + updateOrgs(importDataModel, Objects.requireNonNull(getStringValueFromCell(cell, false)).trim(), donorAgencyCode, session, "donor"); + break; + case "Responsible Organization": + responsibleOrgId = updateOrgs(importDataModel, Objects.requireNonNull(getStringValueFromCell(cell, false)).trim(), responsibleOrgCode, session, "responsibleOrg"); + break; + case "Beneficiary Agency": + responsibleOrgId = updateOrgs(importDataModel, Objects.requireNonNull(getStringValueFromCell(cell, false)).trim(), responsibleOrgCode, session, "beneficiaryAgency"); + break; + case "Funding Item": + setAFundingItemForExcel(sheet, config, row, entry, importDataModel, session, cell, true, true, false,"Actual", fundingItem, existing); + break; + case "Planned Commitment": + setAFundingItemForExcel(sheet, config, row, entry, importDataModel, session, cell, true, false,false, "Planned", fundingItem, existing); + break; + case "Planned Disbursement": + setAFundingItemForExcel(sheet, config, row, entry, importDataModel, session, cell, false, true, false,"Planned", fundingItem, existing); + break; + case "Planned Expenditure": + setAFundingItemForExcel(sheet, config, row, entry, importDataModel, session, cell, false, false,true, "Planned", fundingItem, existing); + break; + case "Actual Commitment": + setAFundingItemForExcel(sheet, config, row, entry, importDataModel, session, cell, true, false, false,"Actual", fundingItem, existing); + break; + case "Actual Disbursement": + setAFundingItemForExcel(sheet, config, row, entry, importDataModel, session, cell, false, true, false,"Actual", fundingItem, existing); + break; + case "Actual Expenditure": + setAFundingItemForExcel(sheet, config, row, entry, importDataModel, session, cell, false, false,true, "Actual", fundingItem, existing); + break; + case "Reporting Date": + default: + logger.error("Unexpected value: " + entry.getValue()); + break; + + } + + + } + fundings.add(fundingItem); + + + } + logger.info("Fundings at this point: {}",fundings); + + + importTheData(importDataModel, session, importedProject, componentName, componentCode, responsibleOrgId, fundings, existing); + + } + } + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/TxtDataImporter.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/TxtDataImporter.java new file mode 100644 index 00000000000..1b5c25f87c8 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/TxtDataImporter.java @@ -0,0 +1,172 @@ +package org.digijava.module.aim.action.dataimporter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.opencsv.CSVParser; +import com.opencsv.CSVParserBuilder; +import com.opencsv.CSVReaderHeaderAware; +import com.opencsv.CSVReaderHeaderAwareBuilder; +import com.opencsv.exceptions.CsvValidationException; +import org.dgfoundation.amp.onepager.util.SessionUtil; +import org.digijava.kernel.persistence.PersistenceManager; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedFilesRecord; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedProject; +import org.digijava.module.aim.action.dataimporter.model.Funding; +import org.digijava.module.aim.action.dataimporter.model.ImportDataModel; +import org.digijava.module.aim.dbentity.AmpActivityVersion; +import org.digijava.module.aim.util.FeaturesUtil; +import org.digijava.module.aim.util.TeamMemberUtil; +import org.hibernate.Session; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static org.digijava.module.aim.action.dataimporter.util.ImporterUtil.*; + +public class TxtDataImporter { + private static final int BATCH_SIZE = 1000; + private static final Logger logger = LoggerFactory.getLogger(TxtDataImporter.class); + + + public static int processTxtFileInBatches(ImportedFilesRecord importedFilesRecord, File file, HttpServletRequest request, Map config, boolean isInternal) + { + logger.info("Processing txt file: " + file.getName()); + CSVParser parser = new CSVParserBuilder().withSeparator(request.getParameter("dataSeparator").charAt(0)).build(); + + try (CSVReaderHeaderAware reader = new CSVReaderHeaderAwareBuilder(new FileReader(file)).withCSVParser(parser).build()) { + List> batch = new ArrayList<>(); + Map values; + int batchNumber =1; + while ((values = reader.readMap()) != null) { + if (isInternal) { + values.put("Donor Agency", FeaturesUtil.getGlobalSettingValue("Internal Ecowas Donor")); + } + batch.add(values); + + if (batch.size() == BATCH_SIZE) { + logger.info("Batch number here: {}",batchNumber); + + // Process the batch + processBatch(batch, request,config,importedFilesRecord); + // Clear the batch for the next set of rows + batch.clear(); + batchNumber+=1; + } + } + + // Process any remaining rows in the batch + if (!batch.isEmpty()) { + logger.info("Processing last batch of size {}", batch.size()); + processBatch(batch, request,config,importedFilesRecord); + } + } catch (IOException | CsvValidationException e) { + logger.error("Error processing txt file "+e.getMessage(),e); + return 0; + } + return 1; + } + + + private static void processBatch(List> batch, HttpServletRequest request,Map config, ImportedFilesRecord importedFilesRecord) throws JsonProcessingException { + logger.info("Processing txt batch"); + SessionUtil.extendSessionIfNeeded(request); + Session session = PersistenceManager.getRequestDBSession(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + + for (Map row : batch) { + ImportedProject importedProject= new ImportedProject(); + importedProject.setImportedFilesRecord(importedFilesRecord); + List fundings= new ArrayList<>(); + + + ImportDataModel importDataModel = new ImportDataModel(); + importDataModel.setModified_by(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeamMemId()); + importDataModel.setCreated_by(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeamMemId()); + importDataModel.setTeam(TeamMemberUtil.getCurrentAmpTeamMember(request).getAmpTeam().getAmpTeamId()); + importDataModel.setIs_draft(true); + OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC); + importDataModel.setCreation_date(now.format(formatter)); + setStatus(importDataModel); + String componentName= row.get(getKey(config, "Component Name")); + String componentCode= row.get(getKey(config, "Component Code")); + String projectCode= row.get(getKey(config, "Project Code")); + String projectTitle= row.get(getKey(config, "Project Title")); + String projectDesc= row.get(getKey(config, "Project Description")); + AmpActivityVersion existing = existingActivity(projectTitle,projectCode,session); + + importDataModel.setProject_title(projectTitle); + importDataModel.setProject_code(projectCode); + importDataModel.setDescription(projectDesc); + + String donorAgencyCode= row.get(getKey(config, "Donor Agency Code")); + String responsibleOrgCode= row.get(getKey(config, "Responsible Organization Code")); + Long responsibleOrgId=null; + + logger.info("Configuration: "+config); + for (Map.Entry entry : config.entrySet()) { + Funding fundingItem = new Funding(); + switch (entry.getValue()) { + case "Project Location": + updateLocations(importDataModel, row.get(entry.getKey().trim()),session); + break; + case "Primary Sector": + updateSectors(importDataModel, row.get(entry.getKey().trim()), session, true); + break; + case "Secondary Sector": + updateSectors(importDataModel, row.get(entry.getKey().trim()), session, false); + break; + case "Donor Agency": + updateOrgs(importDataModel,row.get(entry.getKey().trim()),donorAgencyCode, session, "donor"); + break; + case "Responsible Organization": + responsibleOrgId=updateOrgs(importDataModel,row.get(entry.getKey().trim()),responsibleOrgCode, session, "responsibleOrg"); + break; + case "Beneficiary Agency": + responsibleOrgId=updateOrgs(importDataModel,row.get(entry.getKey().trim()),responsibleOrgCode, session, "beneficiaryAgency"); + break; + case "Funding Item": + setAFundingItemForTxt(config, row, entry, importDataModel, session, Double.parseDouble(row.get(entry.getKey().trim())), true, true, false,"Actual", fundingItem, existing); + break; + case "Planned Commitment": + setAFundingItemForTxt(config, row, entry, importDataModel, session, Double.parseDouble(row.get(entry.getKey().trim())), true, false, false,"Planned", fundingItem, existing); + break; + case "Planned Disbursement": + setAFundingItemForTxt(config, row, entry, importDataModel, session, Double.parseDouble(row.get(entry.getKey().trim())), false, true, false,"Planned", fundingItem, existing); + break; + case "Planned Expenditure": + setAFundingItemForTxt(config, row, entry, importDataModel, session, Double.parseDouble(row.get(entry.getKey().trim())), false, false,true, "Planned", fundingItem, existing); + break; + case "Actual Commitment": + setAFundingItemForTxt(config, row, entry, importDataModel, session, Double.parseDouble(row.get(entry.getKey().trim())), true, false, false,"Actual", fundingItem, existing); + break; + case "Actual Disbursement": + setAFundingItemForTxt(config, row, entry, importDataModel, session, Double.parseDouble(row.get(entry.getKey().trim())), false, true, false,"Actual", fundingItem, existing); + break; + case "Actual Expenditure": + setAFundingItemForTxt(config, row, entry, importDataModel, session, Double.parseDouble(row.get(entry.getKey().trim())), false, false,true, "Actual", fundingItem, existing); + break; + default: + logger.error("Unexpected value: " + entry.getValue()); + break; + } + fundings.add(fundingItem); + logger.info("Funding items :{}",fundings); + + } + + importTheData(importDataModel, session, importedProject, componentName, componentCode,responsibleOrgId,fundings,existing); + + } + + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfig.hbm.xml b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfig.hbm.xml new file mode 100644 index 00000000000..72ae4a20235 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfig.hbm.xml @@ -0,0 +1,18 @@ + + + + + + + DATA_IMPORTER_CONFIG_SEQ + + + + + + + + + + + diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfig.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfig.java new file mode 100644 index 00000000000..ff1357dc6cc --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfig.java @@ -0,0 +1,46 @@ +package org.digijava.module.aim.action.dataimporter.dbentity; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +public class DataImporterConfig implements Serializable { + private Long id; + private String configName; + private Set configValues= new HashSet<>(); + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getConfigName() { + return configName; + } + + public void setConfigName(String configName) { + this.configName = configName; + } + + public Set getConfigValues() { + return configValues; + } + + public void setConfigValues(Set configValues) + { + this.configValues = configValues; + } + + @Override + public String toString() { + return "DataImporterConfig{" + + "id=" + id + + ", configName='" + configName + '\'' + + ", configValues=" + configValues + + '}'; + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfigValues.hbm.xml b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfigValues.hbm.xml new file mode 100644 index 00000000000..2f09f10b099 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfigValues.hbm.xml @@ -0,0 +1,14 @@ + + + + + + + DATA_IMPORTER_CONFIG_VALUES_SEQ + + + + + + + diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfigValues.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfigValues.java new file mode 100644 index 00000000000..9c65ee47c39 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/DataImporterConfigValues.java @@ -0,0 +1,60 @@ +package org.digijava.module.aim.action.dataimporter.dbentity; + +import java.io.Serializable; + +public class DataImporterConfigValues implements Serializable { + private String configKey; + private String configValue; + private DataImporterConfig dataImporterConfig; + private Long id; + + public DataImporterConfigValues() { + + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public DataImporterConfig getDataImporterConfig() { + return dataImporterConfig; + } + + public void setDataImporterConfig(DataImporterConfig dataImporterConfig) { + this.dataImporterConfig = dataImporterConfig; + } + + public DataImporterConfigValues(String configKey, String configValue, DataImporterConfig dataImporterConfig) { + this.configKey = configKey; + this.configValue = configValue; + this.dataImporterConfig = dataImporterConfig; + } + + public String getConfigKey() { + return configKey; + } + + public void setConfigKey(String configKey) { + this.configKey = configKey; + } + + public String getConfigValue() { + return configValue; + } + + public void setConfigValue(String configValue) { + this.configValue = configValue; + } + + @Override + public String toString() { + return "DataImporterConfigValues{" + + "configKey='" + configKey + '\'' + + ", configValue='" + configValue + '\'' + + '}'; + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportStatus.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportStatus.java new file mode 100644 index 00000000000..7fc0d1edbc8 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportStatus.java @@ -0,0 +1,5 @@ +package org.digijava.module.aim.action.dataimporter.dbentity; + +public enum ImportStatus { +UPLOADED,IN_PROGRESS,SUCCESS,FAILED,SKIPPED +} diff --git a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedFilesRecord.hbm.xml b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedFilesRecord.hbm.xml similarity index 81% rename from amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedFilesRecord.hbm.xml rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedFilesRecord.hbm.xml index ca4e4d8f78f..1b4d8649f18 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedFilesRecord.hbm.xml +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedFilesRecord.hbm.xml @@ -2,7 +2,7 @@ - @@ -14,7 +14,7 @@ - + diff --git a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedFilesRecord.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedFilesRecord.java similarity index 94% rename from amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedFilesRecord.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedFilesRecord.java index a8087c06004..6a9a872a3fd 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedFilesRecord.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedFilesRecord.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.dbentity; +package org.digijava.module.aim.action.dataimporter.dbentity; import java.io.Serializable; diff --git a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedProject.hbm.xml b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProject.hbm.xml similarity index 68% rename from amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedProject.hbm.xml rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProject.hbm.xml index a1080a7fa13..33f20208a50 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedProject.hbm.xml +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProject.hbm.xml @@ -2,7 +2,7 @@ - @@ -10,12 +10,12 @@ IMPORTED_PROJECT_SEQ - + - + diff --git a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedProject.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProject.java similarity index 96% rename from amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedProject.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProject.java index 7d95157368e..1db5d34b344 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/dbentity/ImportedProject.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProject.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.dbentity; +package org.digijava.module.aim.action.dataimporter.dbentity; public class ImportedProject { private Long id; diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProjectCurrency.hbm.xml b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProjectCurrency.hbm.xml new file mode 100644 index 00000000000..46b9abd6a59 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProjectCurrency.hbm.xml @@ -0,0 +1,19 @@ + + + + + + + + + IMPORTED_PROJECT_CURRENCY_ID + + + + + + + + + diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProjectCurrency.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProjectCurrency.java new file mode 100644 index 00000000000..d4563f82abf --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/dbentity/ImportedProjectCurrency.java @@ -0,0 +1,33 @@ +package org.digijava.module.aim.action.dataimporter.dbentity; + +import java.io.Serializable; + +public class ImportedProjectCurrency implements Serializable { + private Long id; + private String importedProjectName; + private String currencyCode; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getImportedProjectName() { + return importedProjectName; + } + + public void setImportedProjectName(String importedProjectName) { + this.importedProjectName = importedProjectName; + } + + public String getCurrencyCode() { + return currencyCode; + } + + public void setCurrencyCode(String currencyCode) { + this.currencyCode = currencyCode; + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/ActivityGroup.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/ActivityGroup.java similarity index 77% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/ActivityGroup.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/ActivityGroup.java index d5da88e8a9a..8b602f38a32 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/ActivityGroup.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/ActivityGroup.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; public class ActivityGroup { private Long version; diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Contact.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Contact.java similarity index 90% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/Contact.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Contact.java index b7715c40aca..fef3b2efbda 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Contact.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Contact.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; public class Contact { private Long id; diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/DonorOrganization.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/DonorOrganization.java similarity index 61% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/DonorOrganization.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/DonorOrganization.java index 129e70576c6..ffc75e53e1a 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/DonorOrganization.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/DonorOrganization.java @@ -1,28 +1,11 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; + import java.util.Objects; -public class DonorOrganization { - private Long id; - private Long organization; +public class DonorOrganization extends Organization { private Double percentage; - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getOrganization() { - return organization; - } - - public void setOrganization(Long organization) { - this.organization = organization; - } - public Double getPercentage() { return percentage; } @@ -47,8 +30,8 @@ public int hashCode() { @Override public String toString() { return "DonorOrganization{" + - "id=" + id + - ", organization=" + organization + + "id=" + this.getId() + + ", organization=" + this.getOrganization() + ", percentage=" + percentage + '}'; } diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Funding.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Funding.java similarity index 87% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/Funding.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Funding.java index 2d5b3671144..b584eaa8f4e 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Funding.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Funding.java @@ -1,8 +1,13 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.digijava.module.aim.action.dataimporter.model.Transaction; import java.util.ArrayList; import java.util.List; +@JsonIgnoreProperties("expenditures") public class Funding { private Long funding_id; private Long donor_organization_id; @@ -12,6 +17,10 @@ public class Funding { private Object reporting_date; private List commitments= new ArrayList<>(); private List disbursements= new ArrayList<>(); + + @JsonProperty("expenditures") + private List expenditures= new ArrayList<>(); + private Long type_of_assistance; private Long financing_instrument; private Object funding_status; @@ -50,6 +59,15 @@ public void setActual_completion_date(Object actual_completion_date) { this.actual_completion_date = actual_completion_date; } + public List getExpenditures() { + return expenditures; + } + + public void setExpenditures(List expenditures) { + this.expenditures = expenditures; + } + + public Object getOriginal_completion_date() { return original_completion_date; } diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/ImportDataModel.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/ImportDataModel.java similarity index 88% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/ImportDataModel.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/ImportDataModel.java index 1215b38aaad..90c50bac159 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/ImportDataModel.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/ImportDataModel.java @@ -1,4 +1,6 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; + +import org.digijava.module.aim.dbentity.AmpActivityContact; import java.util.HashSet; import java.util.Set; @@ -6,7 +8,12 @@ public class ImportDataModel { private Long internal_id; private String amp_id; + + + private Long implementation_level; private String project_title; + + private String project_code; private String description; private String objective; private Object document_space; @@ -18,15 +25,16 @@ public class ImportDataModel { private Set secondary_sectors=new HashSet<>(); private Set locations=new HashSet<>(); private Set donor_organization=new HashSet<>(); - private Set responsible_organization=new HashSet<>(); - private Set executing_agency=new HashSet<>(); + private Set responsible_organization=new HashSet<>(); + private Set beneficiary_agency=new HashSet<>(); + private Set executing_agency=new HashSet<>(); private Set activity_internal_ids; private Set fundings= new HashSet<>(); private Set issues; private Long team; private PpcAmount ppc_amount; - private Set donor_contact_information; - private Set project_coordinator_contact_information; + private Set donor_contact_information; + private Set project_coordinator_contact_information; private Set sector_ministry_contact_information; private Set mofed_contact_information; private Set implementing_executing_agency_contact_information; @@ -47,8 +55,7 @@ public class ImportDataModel { private Set activity_documents; private Long activity_status; private Long activity_budget; - private Object implementation_level; - private Object implementation_location; + private Long implementation_location; private Object cris_number; private Set national_plan_objective=new HashSet<>(); private Set primary_programs=new HashSet<>(); @@ -81,7 +88,13 @@ public String getProject_title() { public void setProject_title(String project_title) { this.project_title = project_title; } + public String getProject_code() { + return project_code; + } + public void setProject_code(String project_code) { + this.project_code = project_code; + } public String getDescription() { return description; } @@ -89,6 +102,13 @@ public String getDescription() { public void setDescription(String description) { this.description = description; } + public Set getBeneficiary_agency() { + return beneficiary_agency; + } + + public void setBeneficiary_agency(Set beneficiary_agency) { + this.beneficiary_agency = beneficiary_agency; + } public String getObjective() { return objective; @@ -157,6 +177,9 @@ public void setSecondary_sectors(Set secondary_sectors) { public Set getLocations() { return locations; } + public void setImplementation_level(Long implementation_level) { + this.implementation_level = implementation_level; + } public void setLocations(Set locations) { this.locations = locations; @@ -170,19 +193,19 @@ public void setDonor_organization(Set donor_organization) { this.donor_organization = donor_organization; } - public Set getResponsible_organization() { + public Set getResponsible_organization() { return responsible_organization; } - public void setResponsible_organization(Set responsible_organization) { + public void setResponsible_organization(Set responsible_organization) { this.responsible_organization = responsible_organization; } - public Set getExecuting_agency() { + public Set getExecuting_agency() { return executing_agency; } - public void setExecuting_agency(Set executing_agency) { + public void setExecuting_agency(Set executing_agency) { this.executing_agency = executing_agency; } @@ -226,19 +249,19 @@ public void setPpc_amount(PpcAmount ppc_amount) { this.ppc_amount = ppc_amount; } - public Set getDonor_contact_information() { + public Set getDonor_contact_information() { return donor_contact_information; } - public void setDonor_contact_information(Set donor_contact_information) { + public void setDonor_contact_information(Set donor_contact_information) { this.donor_contact_information = donor_contact_information; } - public Set getProject_coordinator_contact_information() { + public Set getProject_coordinator_contact_information() { return project_coordinator_contact_information; } - public void setProject_coordinator_contact_information(Set project_coordinator_contact_information) { + public void setProject_coordinator_contact_information(Set project_coordinator_contact_information) { this.project_coordinator_contact_information = project_coordinator_contact_information; } @@ -402,19 +425,16 @@ public void setActivity_budget(Long activity_budget) { this.activity_budget = activity_budget; } - public Object getImplementation_level() { + public Long getImplementation_level() { return implementation_level; } - public void setImplementation_level(Object implementation_level) { - this.implementation_level = implementation_level; - } - public Object getImplementation_location() { + public Long getImplementation_location() { return implementation_location; } - public void setImplementation_location(Object implementation_location) { + public void setImplementation_location(Long implementation_location) { this.implementation_location = implementation_location; } @@ -488,6 +508,7 @@ public String toString() { "internal_id=" + internal_id + ", amp_id='" + amp_id + '\'' + ", project_title='" + project_title + '\'' + + ", project_code='" + project_code + '\'' + ", description='" + description + '\'' + ", objective='" + objective + '\'' + ", document_space=" + document_space + diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/InternalId.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/InternalId.java new file mode 100644 index 00000000000..3ecd865cc7e --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/InternalId.java @@ -0,0 +1,13 @@ +package org.digijava.module.aim.action.dataimporter.model; + +public class InternalId extends Organization { + private String internal_id; + + public String getInternal_id() { + return internal_id; + } + + public void setInternal_id(String internal_id) { + this.internal_id = internal_id; + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Location.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Location.java new file mode 100644 index 00000000000..08aa3bb961b --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Location.java @@ -0,0 +1,51 @@ +package org.digijava.module.aim.action.dataimporter.model; + +public class Location { + private Long id; + private Long location; + private Double location_percentage; + + public Location(Long location, Double location_percentage) { + this.location = location; + this.location_percentage = location_percentage; + } + + public Location(Long id, Long location, Double location_percentage) { + this.id = id; + this.location = location; + this.location_percentage = location_percentage; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getLocation() { + return location; + } + + public void setLocation(Long location) { + this.location = location; + } + + public Double getLocation_percentage() { + return location_percentage; + } + + public void setLocation_percentage(Double location_percentage) { + this.location_percentage = location_percentage; + } + + @Override + public String toString() { + return "Location{" + + "id=" + id + + ", location=" + location + + ", location_percentage=" + location_percentage + + '}'; + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Organization.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Organization.java new file mode 100644 index 00000000000..eeeaf3e9c77 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Organization.java @@ -0,0 +1,23 @@ +package org.digijava.module.aim.action.dataimporter.model; + +public class Organization { + private Long id; + private Long organization; + + public Long getOrganization() { + return organization; + } + + public void setOrganization(Long organization) { + this.organization = organization; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} + diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/PpcAmount.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/PpcAmount.java similarity index 91% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/PpcAmount.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/PpcAmount.java index 531d57fb234..57fb2dbd8dd 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/PpcAmount.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/PpcAmount.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; public class PpcAmount { private Object amount; diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Program.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Program.java similarity index 91% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/Program.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Program.java index 93a7b52e1ce..d84bc47ed55 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Program.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Program.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; import java.util.List; diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Sector.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Sector.java similarity index 93% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/Sector.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Sector.java index 0ef219a16cf..396c27e218f 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Sector.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Sector.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; public class Sector { diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Transaction.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Transaction.java similarity index 97% rename from amp/WEB-INF/src/org/digijava/module/admin/util/model/Transaction.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Transaction.java index b1e59b790f4..69537c3e6d9 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/model/Transaction.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/model/Transaction.java @@ -1,4 +1,4 @@ -package org.digijava.module.admin.util.model; +package org.digijava.module.aim.action.dataimporter.model; public class Transaction { private Long transaction_id; diff --git a/amp/WEB-INF/src/org/digijava/module/admin/util/ImportedFileUtil.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/util/ImportedFileUtil.java similarity index 87% rename from amp/WEB-INF/src/org/digijava/module/admin/util/ImportedFileUtil.java rename to amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/util/ImportedFileUtil.java index 9705f8940f2..15ff57dd023 100644 --- a/amp/WEB-INF/src/org/digijava/module/admin/util/ImportedFileUtil.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/util/ImportedFileUtil.java @@ -1,8 +1,8 @@ -package org.digijava.module.admin.util; +package org.digijava.module.aim.action.dataimporter.util; import org.digijava.kernel.persistence.PersistenceManager; -import org.digijava.module.admin.dbentity.ImportStatus; -import org.digijava.module.admin.dbentity.ImportedFilesRecord; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportStatus; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedFilesRecord; import org.hibernate.Query; import org.hibernate.Session; import org.slf4j.Logger; @@ -47,11 +47,11 @@ public static ImportedFilesRecord saveFile(File file, String filename) throws IO return importedFilesRecord; } - public static void updateFileStatus(ImportedFilesRecord importDataModel, ImportStatus status) { + public static void updateFileStatus(ImportedFilesRecord importedFilesRecord, ImportStatus status) { logger.info("Updating file status to " + status); Session session = PersistenceManager.getRequestDBSession(); - importDataModel.setImportStatus(status); - session.save(importDataModel); + importedFilesRecord.setImportStatus(status); + session.saveOrUpdate(importedFilesRecord); session.flush(); } public static List getSimilarFiles(File file) throws IOException, NoSuchAlgorithmException { diff --git a/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/util/ImporterUtil.java b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/util/ImporterUtil.java new file mode 100644 index 00000000000..799cb8dadf9 --- /dev/null +++ b/amp/WEB-INF/src/org/digijava/module/aim/action/dataimporter/util/ImporterUtil.java @@ -0,0 +1,1155 @@ +package org.digijava.module.aim.action.dataimporter.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.format.CellFormatType; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.digijava.kernel.ampapi.endpoints.activity.ActivityImportRules; +import org.digijava.kernel.ampapi.endpoints.activity.ActivityInterchangeUtils; +import org.digijava.kernel.ampapi.endpoints.activity.dto.ActivitySummary; +import org.digijava.kernel.ampapi.endpoints.common.JsonApiResponse; +import org.digijava.kernel.persistence.PersistenceManager; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportStatus; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedProject; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedProjectCurrency; +import org.digijava.module.aim.action.dataimporter.model.*; +import org.digijava.module.aim.dbentity.*; +import org.digijava.module.aim.util.CurrencyUtil; +import org.digijava.module.categorymanager.dbentity.AmpCategoryValue; +import org.digijava.module.categorymanager.util.CategoryConstants; +import org.hibernate.Query; +import org.hibernate.Session; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static com.fasterxml.jackson.core.JsonGenerator.Feature.ESCAPE_NON_ASCII; + +public class ImporterUtil { + public static Map ConstantsMap = new HashMap<>(); + + private static final int BATCH_SIZE = 1000; + private static final Logger logger = LoggerFactory.getLogger(ImporterUtil.class); + + private static Double parseDouble(String number) { + try { + return Double.parseDouble(number); + } catch (NumberFormatException e) { + //not a double + return null; + } + + } + + public static Funding setAFundingItemForExcel(Sheet sheet, Map config, Row row, Map.Entry entry, ImportDataModel importDataModel, Session session, Cell cell, boolean commitment, boolean disbursement, boolean expenditure, String + adjustmentType, Funding fundingItem, AmpActivityVersion existingActivity) { + int detailColumn = getColumnIndexByName(sheet, getKey(config, "Financing Instrument")); + String finInstrument = detailColumn >= 0 ? getStringValueFromCell(row.getCell(detailColumn), false) : ""; + + detailColumn = getColumnIndexByName(sheet, getKey(config, "Exchange Rate")); + String exchangeRate = detailColumn >= 0 ? getStringValueFromCell(row.getCell(detailColumn), false) : ""; + Double exchangeRateValue = !exchangeRate.isEmpty() ? parseDouble(exchangeRate) : Double.valueOf(0.0); + + detailColumn = getColumnIndexByName(sheet, getKey(config, "Type Of Assistance")); + String typeOfAss = detailColumn >= 0 ? getStringValueFromCell(row.getCell(detailColumn), false) : ""; + int separateFundingDateColumn = getColumnIndexByName(sheet, getKey(config, "Transaction Date")); + String separateFundingDate = separateFundingDateColumn >= 0 ? getDateFromExcel(row, separateFundingDateColumn) : null; + + int currencyCodeColumn = getColumnIndexByName(sheet, getKey(config, "Currency")); + String currencyCode = currencyCodeColumn >= 0 ? getStringValueFromCell(row.getCell(currencyCodeColumn), true) : CurrencyUtil.getDefaultCurrency().getCurrencyCode(); + if (existingActivity != null) { + String existingActivityCurrencyCode = getCurrencyCodeFromExistingImported(existingActivity.getName()); + if (existingActivityCurrencyCode != null) { + currencyCode = existingActivityCurrencyCode; + } + } + saveCurrencyCode(currencyCode, importDataModel.getProject_title()); + Funding funding; + int componentNameColumn = getColumnIndexByName(sheet, getKey(config, "Component Name")); + String componentName = componentNameColumn >= 0 ? getStringValueFromCell(row.getCell(componentNameColumn), true) : null; + if (importDataModel.getDonor_organization() == null || importDataModel.getDonor_organization().isEmpty()) { + if (!config.containsValue("Donor Agency")) { + funding = updateFunding(fundingItem, importDataModel, getNumericValueFromCell(cell), entry.getKey(), separateFundingDate, getRandomOrg(session), typeOfAss, finInstrument, commitment, disbursement, expenditure, adjustmentType, currencyCode, componentName, exchangeRateValue); + + } else { + int columnIndex1 = getColumnIndexByName(sheet, getKey(config, "Donor Agency")); + int donorAgencyCodeColumn = getColumnIndexByName(sheet, getKey(config, "Donor Agency Code")); + String donorAgencyCode = donorAgencyCodeColumn >= 0 ? getStringValueFromCell(row.getCell(donorAgencyCodeColumn), true) : null; + updateOrgs(importDataModel, columnIndex1 >= 0 ? Objects.requireNonNull(getStringValueFromCell(row.getCell(columnIndex1), false)).trim() : "no org", donorAgencyCode, session, "donor"); + funding = updateFunding(fundingItem, importDataModel, getNumericValueFromCell(cell), entry.getKey(), separateFundingDate, new ArrayList<>(importDataModel.getDonor_organization()).get(0).getOrganization(), typeOfAss, finInstrument, commitment, disbursement, expenditure, adjustmentType, currencyCode, componentName, exchangeRateValue); + } + + } else { + funding = updateFunding(fundingItem, importDataModel, getNumericValueFromCell(cell), entry.getKey(), separateFundingDate, new ArrayList<>(importDataModel.getDonor_organization()).get(0).getOrganization(), typeOfAss, finInstrument, commitment, disbursement, expenditure, adjustmentType, currencyCode, componentName, exchangeRateValue); + } + return funding; + } + + + public static Funding setAFundingItemForTxt(Map row, Map config, Map.Entry entry, ImportDataModel importDataModel, Session session, Number value, boolean commitment, boolean disbursement, boolean expenditure, String + adjustmentType, Funding fundingItem, AmpActivityVersion existingActivity) { + String finInstrument = row.get(getKey(config, "Financing Instrument")); + finInstrument = finInstrument != null ? finInstrument : ""; + + String typeOfAss = row.get(getKey(config, "Type Of Assistance")); + typeOfAss = typeOfAss != null ? typeOfAss : ""; + Funding funding; + + String separateFundingDate = row.get(getKey(config, "Transaction Date")); + separateFundingDate = separateFundingDate != null ? separateFundingDate : ""; + + String currencyCode = row.get(getKey(config, "Currency")); + currencyCode = currencyCode != null ? currencyCode : CurrencyUtil.getDefaultCurrency().getCurrencyCode(); + if (existingActivity != null) { + String existingActivityCurrencyCode = getCurrencyCodeFromExistingImported(existingActivity.getName()); + if (existingActivityCurrencyCode != null) { + currencyCode = existingActivityCurrencyCode; + } + } + saveCurrencyCode(currencyCode, importDataModel.getProject_title()); + String componentName = row.get(getKey(config, "Component Name")); + componentName = componentName != null ? componentName : ""; + + + String exchangeRate = row.get(getKey(config, "Exchange Rate")); + exchangeRate = exchangeRate != null ? exchangeRate : ""; + + Double exchangeRateValue = !exchangeRate.isEmpty() ? parseDouble(exchangeRate) : Double.valueOf(0.0); + + + if (importDataModel.getDonor_organization() == null || importDataModel.getDonor_organization().isEmpty()) { + if (!config.containsValue("Donor Agency")) { + funding = updateFunding(fundingItem, importDataModel, value, entry.getKey(), separateFundingDate, getRandomOrg(session), typeOfAss, finInstrument, commitment, disbursement, expenditure, adjustmentType, currencyCode, componentName, exchangeRateValue); + + } else { + String donorColumn = row.get(getKey(config, "Donor Agency")); + String donorAgencyCode = row.get(getKey(config, "Donor Agency Code")); + + updateOrgs(importDataModel, donorColumn != null && !donorColumn.isEmpty() ? donorColumn.trim() : "no org", donorAgencyCode, session, "donor"); + funding = updateFunding(fundingItem, importDataModel, value, entry.getKey(), separateFundingDate, new ArrayList<>(importDataModel.getDonor_organization()).get(0).getOrganization(), typeOfAss, finInstrument, commitment, disbursement, expenditure, adjustmentType, currencyCode, componentName, exchangeRateValue); + } + + } else { + funding = updateFunding(fundingItem, importDataModel, value, entry.getKey(), separateFundingDate, new ArrayList<>(importDataModel.getDonor_organization()).get(0).getOrganization(), typeOfAss, finInstrument, commitment, disbursement, expenditure, adjustmentType, currencyCode, componentName, exchangeRateValue); + } + return funding; + } + + public static String getStringValueFromCell(Cell cell, boolean nullable) { + try { + if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { + return String.valueOf(cell.getNumericCellValue()); + } + if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { + return String.valueOf(cell.getBooleanCellValue()); + } + if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { + return String.valueOf(cell.getCellFormula()); + } + return cell.getStringCellValue(); + } catch (Exception e) { + logger.error("Error getting cell {} value: ", cell, e); + return nullable ? null : ""; + } + } + + public static Number getNumericValueFromCell(Cell cell) { + try { + return cell.getNumericCellValue(); + } catch (Exception e) { + logger.error("Error getting cell {} value: ", cell, e); + return 0; + } + } + + private static String getDateFromExcel(Row row, int columnIndex) { + Cell cell = row.getCell(columnIndex); // Assuming the date is in the first column + try { + cell.setCellType(Cell.CELL_TYPE_STRING); + logger.info("Date TYpe: " + cell.getCellType()); +// if (DateUtil.isCellDateFormatted(cell)) { + String date = cell.getStringCellValue(); + String formattedDate = formatDateFromDateObject(date); + logger.info("Formatted Date: " + formattedDate); + return formattedDate; +// } else { +// logger.info("The cell does not contain a valid date."); +// return null; +// } + } catch (Exception e) { + logger.error("Error parsing date column", e); + return null; + } + + } + + private static void saveCurrencyCode(String currencyCode, String projectName) { + Session session = getSession(); + + String hql = "FROM " + ImportedProjectCurrency.class.getName() + " ipc WHERE ipc.importedProjectName = :importedProjectName"; + Query query = session.createQuery(hql); + query.setParameter("importedProjectName", projectName); + List importedProjectCurrencies = query.list(); + + if (importedProjectCurrencies.isEmpty()) { + ImportedProjectCurrency importedProjectCurrency = new ImportedProjectCurrency(); + importedProjectCurrency.setCurrencyCode(currencyCode); + importedProjectCurrency.setImportedProjectName(projectName); + session.saveOrUpdate(importedProjectCurrency); + } else { + importedProjectCurrencies.get(0).setCurrencyCode(currencyCode); + session.update(importedProjectCurrencies.get(0)); + } + +// session.flush(); + } + + private static String getCurrencyCodeFromExistingImported(String importedProjectName) { + Session session = getSession(); + String hql = "FROM " + ImportedProjectCurrency.class.getName() + " ipc where ipc.importedProjectName= :importedProjectName"; + Query query = session.createQuery(hql); + query.setParameter("importedProjectName", importedProjectName); + List importedProjectCurrencies = query.list(); + return importedProjectCurrencies != null && !importedProjectCurrencies.isEmpty() ? importedProjectCurrencies.get(0).getCurrencyCode() : null; + } + + @NotNull + private static Session getSession() { + Session session = PersistenceManager.getRequestDBSession(); + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + return session; + } + + + private static String getFundingDate(String dateString) { + LocalDate date = LocalDate.now(); + if (isCommonDateFormat(dateString)) { + List formatters = Arrays.asList( + DateTimeFormatter.ofPattern("yyyy-MM-dd"), + DateTimeFormatter.ofPattern("dd/MM/yyyy"), + DateTimeFormatter.ofPattern("MM/dd/yyyy"), + DateTimeFormatter.ofPattern("MM-dd-yyyy"), + DateTimeFormatter.ofPattern("yyyy/MM/dd"), + DateTimeFormatter.ofPattern("dd-MM-yyyy"), + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"), + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"), + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS"), + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"), + DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy") + ); + + for (DateTimeFormatter formatter : formatters) { + try { + date = LocalDate.parse(dateString, formatter); + break; + } catch (DateTimeParseException e) { + // Continue to next formatter + } + } + } else { + if (StringUtils.isNumeric(dateString)) { + date = LocalDate.of(Integer.parseInt(dateString), 1, 1); + } else { + throw new IllegalArgumentException("Invalid year format: " + dateString); + } + } + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + return date.format(formatter); + } + + + private static String formatDateFromDateObject(String date) { + List formatters = Arrays.asList( + new SimpleDateFormat("yyyy-MM-dd"), + new SimpleDateFormat("dd/MM/yyyy"), + new SimpleDateFormat("MM/dd/yyyy"), + new SimpleDateFormat("MM-dd-yyyy"), + new SimpleDateFormat("yyyy/MM/dd"), + new SimpleDateFormat("dd-MM-yyyy"), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS"), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") + ); + + String formattedDate = null; + + // Check if date is in year-only format (e.g., "2024") + if (Pattern.matches("\\d{4}", date)) { + try { + // Parse the year and create a Date object for January 1 of that year + Date januaryFirst = new SimpleDateFormat("yyyy-MM-dd").parse(date + "-01-01"); + return new SimpleDateFormat("yyyy-MM-dd").format(januaryFirst); // Return as "yyyy-MM-dd" + } catch (ParseException e) { + // Log error if needed, or handle exception for invalid date format + } + } + + // Try other date formats if not year-only + for (SimpleDateFormat formatter : formatters) { + try { + Date parsedDate = formatter.parse(date); + formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(parsedDate); // Convert to "yyyy-MM-dd" + break; + } catch (ParseException e) { + // Continue to next formatter + } + } + + return formattedDate; + } + + + public static boolean isCommonDateFormat(String dateString) { + List dateFormats = Arrays.asList( + "yyyy-MM-dd", + "dd-MM-yyyy", + "MM-dd-yyyy", + "MM/dd/yyyy", + "dd/MM/yyyy", + "dd.MM.yyyy", + "yyyy/MM/dd", + "yyyy-MM-dd HH:mm:ss.S", + "yyyy-MM-dd HH:mm:ss", + "yyyy-MM-dd HH:mm:ss.SSS", + "EEE MMM dd HH:mm:ss yyyy" + ); + + for (String dateFormat : dateFormats) { + try { + LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat)); + return true; + } catch (Exception e) { + // Ignore and continue with the next format + } + } + + return false; + } + + public static K getKey(Map map, V value) { + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue().equals(value)) { + return entry.getKey(); + } + } + return null; + } + + public static String findYearSubstring(String text) { + Pattern pattern = Pattern.compile("(?:19|20)\\d{2}"); + Matcher matcher = pattern.matcher(text); + if (matcher.find()) { + return matcher.group(); + } else { + return null; + } + } + + public static void removeMapItem(Map map, String columnName, String selectedField) { + // Check if the entry's key and value match the criteria + // Remove the entry + map.entrySet().removeIf(entry -> columnName.equals(entry.getKey()) && selectedField.equals(entry.getValue())); + } + + + public static boolean isFileReadable(File file) { + if (file == null || !file.exists() || !file.isFile()) { + return false; + } + return file.canRead(); + } + + // Check if the file content is valid + public static boolean isFileContentValid(File file) { + // Define your validation criteria here + // For example, let's say we want to check if the file contains at least one line + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String line = reader.readLine(); + return line != null; // If at least one line exists, consider the content valid + } catch (IOException e) { + e.printStackTrace(); // Handle the exception appropriately + return false; // Consider the content invalid if an exception occurs + } + } + + + private static Funding updateFunding(Funding fundingItem, ImportDataModel importDataModel, Number amount, String columnHeaderContainingYear, String separateFundingDate, Long orgId, String assistanceType, String finInst, boolean commitment, boolean disbursement, boolean expenditure, String + adjustmentType, String currencyCode, String componentName, Double exchangeRate) { + // TODO: 27/06/2024 pick Month from file and use it in funding + Session session = getSession(); + Long currencyId = getCurrencyId(session, currencyCode); + Long adjType = getCategoryValue("adjustmentType", CategoryConstants.ADJUSTMENT_TYPE_KEY, adjustmentType); + Long assType = getCategoryValue("assistanceType", CategoryConstants.TYPE_OF_ASSISTENCE_KEY, assistanceType); + Long finInstrument = getCategoryValue("finInstrument", CategoryConstants.FINANCING_INSTRUMENT_KEY, finInst); + Long orgRole = getOrganizationRole(session); + + + String yearString; + String fundingDate; + if (separateFundingDate != null) { + if (isCommonDateFormat(separateFundingDate)) { + fundingDate = getFundingDate(separateFundingDate); + } else { + yearString = findYearSubstring(separateFundingDate); + fundingDate = yearString != null ? getFundingDate(yearString) : getFundingDate("2000"); + + } + } else { + yearString = findYearSubstring(columnHeaderContainingYear); + fundingDate = yearString != null ? getFundingDate(yearString) : getFundingDate("2000"); + + } + + fundingItem.setDonor_organization_id(orgId); + fundingItem.setType_of_assistance(assType); + fundingItem.setFinancing_instrument(finInstrument); + fundingItem.setSource_role(orgRole); + + Transaction transaction = new Transaction(); + transaction.setCurrency(currencyId); + transaction.setAdjustment_type(adjType); + transaction.setTransaction_amount(amount != null ? amount.doubleValue() : 0.0); + transaction.setTransaction_date(fundingDate); + transaction.setFixed_exchange_rate(exchangeRate); + if (commitment) { + fundingItem.getCommitments().add(transaction); + } + if (disbursement) { + fundingItem.getDisbursements().add(transaction); + } + if (expenditure) { + if (transaction.getTransaction_amount() == 0) { + transaction.setTransaction_amount(-1); + } + if (transaction.getTransaction_amount() > 0) { + transaction.setTransaction_amount(-transaction.getTransaction_amount()); + } + fundingItem.getCommitments().add(transaction); + + } + + + DonorOrganization donorOrganization = new DonorOrganization(); + donorOrganization.setOrganization(orgId); + donorOrganization.setPercentage(100.0); + + importDataModel.getDonor_organization().add(donorOrganization); + if (componentName == null || componentName.isEmpty()) { + importDataModel.getFundings().add(fundingItem); + + } + return fundingItem; + } + + private static Long getOrganizationRole(Session session) { + + if (ConstantsMap.containsKey("orgRole")) { + Long val = ConstantsMap.get("orgRole"); + logger.info("In cache... orgRole: " + val); + return val; + + } + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = "SELECT o.ampRoleId FROM " + AmpRole.class.getName() + " o WHERE LOWER(o.name) LIKE LOWER(:name)"; + + Query query = session.createQuery(hql); + query.setParameter("name", "%donor%"); + List orgRoles = query.list(); + Long orgRole = orgRoles.get(0); + ConstantsMap.put("orgRole", orgRole); + return orgRole; + } + + private static Long getCurrencyId(Session session, String currencyCode) { + + if (ConstantsMap.containsKey("currencyId")) { + Long val = ConstantsMap.get("currencyId"); + logger.info("In cache... currency: " + val); + return val; + + } + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + if (currencyCode == null) { + currencyCode = "USD"; + } + String hql = "SELECT ac.ampCurrencyId FROM " + AmpCurrency.class.getName() + " ac " + + "WHERE ac.currencyCode = :currencyCode"; + + Query query = session.createQuery(hql); + query.setString("currencyCode", currencyCode); + Long currencyId = (Long) query.uniqueResult(); + ConstantsMap.put("currencyId", currencyId); + return currencyId; + } + + private static Long getCategoryValue(String constantKey, String categoryKey, String possibleValue) { + String hql = "SELECT s FROM " + AmpCategoryValue.class.getName() + " s JOIN s.ampCategoryClass c WHERE c.keyName = :categoryKey"; + String fullKey = constantKey + "_" + possibleValue; + if (ConstantsMap.containsKey(fullKey)) { + Long val = ConstantsMap.get(fullKey); + logger.info("In cache... " + fullKey + ":" + val); + return val; + } + Session session = getSession(); + Query query = session.createQuery(hql); + query.setParameter("categoryKey", categoryKey); + List values = query.list(); + Long categoryId = ((AmpCategoryValue) values.get(0)).getId(); + + if (!Objects.equals(possibleValue, "") && !Objects.equals(possibleValue, null)) { + for (Object categoryValue : values) { + if (Objects.equals(((AmpCategoryValue) categoryValue).getValue().toLowerCase(), possibleValue.toLowerCase())) { + categoryId = ((AmpCategoryValue) categoryValue).getId(); + logger.info("Found category: " + ((AmpCategoryValue) categoryValue).getValue()); + break; + } + + } + } + logger.info("Found category: " + categoryId + " for " + constantKey + "_" + possibleValue); + ConstantsMap.put(fullKey, categoryId); + return categoryId; + } + + public static AmpActivityVersion existingActivity(String projectTitle, String projectCode, Session session) { + if ((projectTitle == null || projectTitle.trim().isEmpty()) && + (projectCode == null || projectCode.trim().isEmpty())) { + return null; + } + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = "SELECT a FROM " + AmpActivityVersion.class.getName() + " a " + + "WHERE a.name = :name"; + Query query = session.createQuery(hql); + query.setCacheable(true); + query.setString("name", projectTitle); +// query.setString("projectCode", projectCode); + List ampActivityVersions = query.list(); + return !ampActivityVersions.isEmpty() ? ampActivityVersions.get(ampActivityVersions.size() - 1) : null; + } + + public static void setStatus(ImportDataModel importDataModel) { + Long statusId = getCategoryValue("statusId", CategoryConstants.ACTIVITY_STATUS_KEY, ""); + importDataModel.setActivity_status(statusId); + } + + public static void importTheData(ImportDataModel importDataModel, Session session, ImportedProject importedProject, String componentName, String componentCode, Long responsibleOrgId, List fundings, AmpActivityVersion existing) throws JsonProcessingException { + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + ActivityImportRules rules = new ActivityImportRules(true, false, + true); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(ESCAPE_NON_ASCII, false); // Disable escaping of non-ASCII characters during serialization + objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); + + Map map = objectMapper + .convertValue(importDataModel, new TypeReference>() { + }); + JsonApiResponse response; + logger.info("Existing ?" + existing); + logger.info("Data model object: " + importDataModel); + if (importDataModel.getProject_title().trim().isEmpty() && importDataModel.getProject_code().trim().isEmpty()) { + logger.info("Project title and code are empty. Skipping import"); + importedProject.setImportStatus(ImportStatus.SKIPPED); + return; + } + if (existing == null) { + logger.info("New activity"); + importedProject.setNewProject(true); + response = ActivityInterchangeUtils.importActivity(map, false, rules, "activity/new"); + } else { + logger.info("Existing activity"); + importedProject.setNewProject(false); + importDataModel.setInternal_id(existing.getAmpActivityId()); + importDataModel.setAmp_id(existing.getAmpId()); + ActivityGroup activityGroup = new ActivityGroup(); + activityGroup.setVersion(existing.getAmpActivityGroup().getVersion()); + importDataModel.setActivity_group(activityGroup); + importDataModel.setProject_title(existing.getName()); + importDataModel.setProject_code(!Objects.equals(importDataModel.getProject_code(), "") ? importDataModel.getProject_code() : existing.getProjectCode()); + updateFundingAndOrgsWithAlreadyExisting(existing, importDataModel); + map = objectMapper + .convertValue(importDataModel, new TypeReference>() { + }); + response = ActivityInterchangeUtils.importActivity(map, true, rules, "activity/update"); + } + if (response != null) { + if (!response.getErrors().isEmpty()) { + importedProject.setImportStatus(ImportStatus.FAILED); + } else { + importedProject.setImportStatus(ImportStatus.SUCCESS); + logger.info("Successfully imported the project. Now adding component if present"); + logger.info("--------------------------------"); + logger.info("Component name at start: " + componentName); + if (componentName != null && !componentName.isEmpty()) { + addComponentsAndProjectCode(response, componentName, componentCode, responsibleOrgId, fundings, importDataModel.getProject_code()); + } +// logger.info("Updating expenditures ................"); +// updateExpendituresIfAny(response); + + } + } + + String resp = objectMapper.writeValueAsString(response); + importedProject.setImportResponse(resp); + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + session.saveOrUpdate(importedProject); + session.flush(); + + logger.info("Imported project: " + importedProject); + } + + private static void updateFundingAndOrgsWithAlreadyExisting(AmpActivityVersion ampActivityVersion, ImportDataModel importDataModel) { + + if (ampActivityVersion.getFunding() != null) { + Long adjType = getCategoryValue("adjustmentType", CategoryConstants.ADJUSTMENT_TYPE_KEY, ""); + Long assType = getCategoryValue("assistanceType", CategoryConstants.TYPE_OF_ASSISTENCE_KEY, ""); + Long finInstrument = getCategoryValue("finInstrument", CategoryConstants.FINANCING_INSTRUMENT_KEY, ""); + for (AmpFunding ampFunding : ampActivityVersion.getFunding()) { + Funding funding = new Funding(); + funding.setDonor_organization_id(ampFunding.getAmpDonorOrgId().getAmpOrgId()); + funding.setType_of_assistance(ampFunding.getTypeOfAssistance() != null ? ampFunding.getTypeOfAssistance().getId() : assType); + funding.setFinancing_instrument(ampFunding.getFinancingInstrument() != null ? ampFunding.getFinancingInstrument().getId() : finInstrument); + funding.setSource_role(ampFunding.getSourceRole().getAmpRoleId()); + for (AmpFundingDetail ampFundingDetail : ampFunding.getFundingDetails()) { + Transaction transaction = new Transaction(); + transaction.setCurrency(ampFundingDetail.getAmpCurrencyId().getAmpCurrencyId()); + transaction.setAdjustment_type(ampFundingDetail.getAdjustmentType() != null ? ampFundingDetail.getAdjustmentType().getId() : adjType); + transaction.setTransaction_amount(ampFundingDetail.getTransactionAmount()); + if (ampFundingDetail.getTransactionDate() != null) { + + transaction.setTransaction_date(getFundingDate(ampFundingDetail.getTransactionDate().toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate().toString())); + } + transaction.setFixed_exchange_rate(ampFundingDetail.getFixedExchangeRate()); + if (ampFundingDetail.getTransactionType() == 0) { + funding.getCommitments().add(transaction); + } else if (ampFundingDetail.getTransactionType() == 1) { + funding.getDisbursements().add(transaction); + } + } + + } + } + if (ampActivityVersion.getOrgrole() != null && !ampActivityVersion.getOrgrole().isEmpty()) { + double perc = (double) 100 / ampActivityVersion.getOrgrole().size(); + for (AmpOrgRole ampOrgRole : ampActivityVersion.getOrgrole()) { + if (ampOrgRole.getRole().getRoleCode().equalsIgnoreCase("DN")) { + DonorOrganization donorOrganization = new DonorOrganization(); + donorOrganization.setOrganization(ampOrgRole.getOrganisation().getAmpOrgId()); + donorOrganization.setPercentage(ampOrgRole.getPercentage() != null ? (double) ampOrgRole.getPercentage() : perc); + importDataModel.getDonor_organization().add(donorOrganization); + } else if (ampOrgRole.getRole().getRoleCode().equalsIgnoreCase("EA")) { + Organization responsibleOrg = new Organization(); + responsibleOrg.setOrganization(ampOrgRole.getOrganisation().getAmpOrgId()); + importDataModel.getResponsible_organization().add(responsibleOrg); + } else if (ampOrgRole.getRole().getRoleCode().equalsIgnoreCase("BA")) { + Organization beneficiaryAgency = new Organization(); + beneficiaryAgency.setOrganization(ampOrgRole.getOrganisation().getAmpOrgId()); + importDataModel.getBeneficiary_agency().add(beneficiaryAgency); + + } + } + } + } + + static void updateExpendituresIfAny(JsonApiResponse response) { + Long activityId = (Long) response.getContent().getAmpActivityId(); + Session session = PersistenceManager.getRequestDBSession(); + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = "FROM " + AmpActivityVersion.class.getName() + " a WHERE a.ampActivityId= :activityId"; + Query query = session.createQuery(hql); + query.setParameter("activityId", activityId); +// query.setMaxResults(1); + List activityVersions = query.list(); + if (activityVersions != null && !activityVersions.isEmpty()) { + Set ampFundings = activityVersions.get(activityVersions.size() - 1).getFunding(); + logger.info("Activity Fundings found: " + ampFundings); + for (AmpFunding ampFunding : ampFundings) { + for (AmpFundingDetail ampFundingDetail : ampFunding.getFundingDetails()) { + if (ampFundingDetail.getTransactionAmount() < 0) { + ampFundingDetail.setTransactionType(2); + if (ampFundingDetail.getTransactionAmount() == -1) { + ampFundingDetail.setTransactionAmount(0.0); + } + ampFundingDetail.setTransactionAmount(Math.abs(ampFundingDetail.getTransactionAmount())); + session.saveOrUpdate(ampFundingDetail); + logger.info("AmpFunding refund: " + ampFundingDetail); + } + } + + } + Set components = activityVersions.get(activityVersions.size() - 1).getComponents(); + logger.info("Components: " + components); + for (AmpComponent ampComponent : components) { + for (AmpComponentFunding ampComponentFunding : ampComponent.getFundings()) { + if (ampComponentFunding.getTransactionAmount() < 0) { + ampComponentFunding.setTransactionType(2); + if (ampComponentFunding.getTransactionAmount() == -1) { + ampComponentFunding.setTransactionAmount(0.0); + } + ampComponentFunding.setTransactionAmount(Math.abs(ampComponentFunding.getTransactionAmount())); + session.saveOrUpdate(ampComponentFunding); + logger.info("AmpComponent expenditure: " + ampComponentFunding); + } + } + } + } + } + + + static void addComponentsAndProjectCode(JsonApiResponse response, String componentName, String componentCode, Long responsibleOrgId, List fundings, String projectCode) { + Long activityId = (Long) response.getContent().getAmpActivityId(); + Session session = getSession(); + + AmpActivityVersion ampActivityVersion = getActivityVersion(session, activityId); + + if (ampActivityVersion != null) { + AmpComponent ampComponent = getOrCreateComponent(ampActivityVersion, componentName, componentCode); + ampComponent.setActivity(ampActivityVersion); + processFundings(ampComponent, fundings, responsibleOrgId); + + boolean updateActivity = updateProjectCodeIfNeeded(ampActivityVersion, projectCode); + + saveOrUpdateComponent(session, ampActivityVersion, ampComponent, updateActivity); + } + } + + + private static AmpActivityVersion getActivityVersion(Session session, Long activityId) { + String hql = "FROM " + AmpActivityVersion.class.getName() + " a WHERE a.ampActivityId= :activityId"; + Query query = session.createQuery(hql); + query.setParameter("activityId", activityId); + query.setMaxResults(1); + return (AmpActivityVersion) query.uniqueResult(); + } + + private static AmpComponent getOrCreateComponent(AmpActivityVersion ampActivityVersion, String componentName, String componentCode) { + return ampActivityVersion.getComponents().stream() + .filter(c -> StringUtils.equalsIgnoreCase(c.getTitle(), componentName)) + .findFirst() + .orElseGet(() -> { + AmpComponent newComponent = new AmpComponent(); + newComponent.setTitle(componentName); + newComponent.setCode(componentCode); + return newComponent; + }); + } + + private static void processFundings(AmpComponent ampComponent, List fundings, Long responsibleOrgId) { + for (Funding funding : new HashSet<>(fundings)) { + if (funding != null && funding.getDonor_organization_id() != null) { + processTransactions(ampComponent, funding.getCommitments(), responsibleOrgId, 0); + processTransactions(ampComponent, funding.getDisbursements(), responsibleOrgId, 1); + processTransactions(ampComponent, funding.getExpenditures(), responsibleOrgId, 2); + } + } + } + + private static void processTransactions(AmpComponent ampComponent, List transactions, Long responsibleOrgId, int defaultType) { + for (Transaction transaction : transactions) { + AmpComponentFunding ampComponentFunding = createFunding(ampComponent, transaction, responsibleOrgId, defaultType); + if (!componentFundingExists(ampComponentFunding, ampComponent)) { + ampComponent.getFundings().add(ampComponentFunding); + } + } + } + + private static AmpComponentFunding createFunding(AmpComponent ampComponent, Transaction transaction, Long responsibleOrgId, int defaultType) { + AmpComponentFunding funding = new AmpComponentFunding(); + funding.setComponent(ampComponent); + funding.setReportingDate(new Date()); + if (responsibleOrgId != null) { + funding.setReportingOrganization(getAmpOrganisationById(responsibleOrgId)); + } + funding.setTransactionAmount(transaction.getTransaction_amount()); + funding.setTransactionType(transaction.getTransaction_amount() < 0 ? 2 : defaultType); + funding.setCurrency(getAmpCurrencyById(transaction.getCurrency())); + funding.setAdjustmentType(getCategoryValueObjectById(transaction.getAdjustment_type())); + funding.setTransactionDate(convertStringToDate(transaction.getTransaction_date())); + return funding; + } + + private static boolean updateProjectCodeIfNeeded(AmpActivityVersion ampActivityVersion, String projectCode) { + if (projectCode != null && !projectCode.isEmpty()) { + ampActivityVersion.setProjectCode(projectCode); + return true; + } + return false; + } + + private static void saveOrUpdateComponent(Session session, AmpActivityVersion ampActivityVersion, AmpComponent ampComponent, boolean updateActivity) { + if (!ampActivityVersion.getComponents().contains(ampComponent)) { + ampActivityVersion.getComponents().add(ampComponent); + updateActivity = true; + } + if (updateActivity) { + session.saveOrUpdate(ampActivityVersion); + } else { + session.saveOrUpdate(ampComponent); + } + } + + + private static boolean componentFundingExists(AmpComponentFunding ampComponentFunding, AmpComponent ampComponent) { + logger.info("AmpComponentFunding to search: " + ampComponentFunding); + + if (ampComponent.getAmpComponentId() == null) { + logger.info("Component is null"); + return false; + } + for (AmpComponentFunding ampComponentFunding1 : ampComponent.getFundings()) { + logger.info("AmpComponentFunding here: " + ampComponentFunding); + + if (Objects.equals(ampComponentFunding.getTransactionAmount(), ampComponentFunding1.getTransactionAmount()) && Objects.equals(ampComponentFunding.getTransactionDate(), ampComponentFunding1.getTransactionDate()) && Objects.equals(ampComponentFunding.getAdjustmentType(), ampComponentFunding1.getAdjustmentType()) && Objects.equals(ampComponentFunding.getReportingOrganization(), ampComponentFunding1.getReportingOrganization())) { + logger.info("AmpComponentFunding has been found"); + + return true; + } + } + logger.info("AmpComponentFunding not found: " + ampComponentFunding); + + + return false; + } + + public static Date convertStringToDate(String dateString) { + SimpleDateFormat[] formats = { + new SimpleDateFormat("dd/MM/yyyy"), + new SimpleDateFormat("MM/dd/yyyy"), + new SimpleDateFormat("yyyy-MM-dd"), + new SimpleDateFormat("MM-dd-yyyy"), + new SimpleDateFormat("yyyy/MM/dd"), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS"), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"), + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), + new SimpleDateFormat("dd-MM-yyyy") + }; + + + for (SimpleDateFormat format : formats) { + try { + return format.parse(dateString); + } catch (ParseException e) { + // Try the next format + } + } + + // If none of the formats matched, return null + return null; + + + } + + private static AmpCategoryValue getCategoryValueObjectById(Long id) { + Session session = PersistenceManager.getRequestDBSession(); + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = "FROM " + AmpCategoryValue.class.getName() + " a " + + "WHERE a.id = :id"; + Query query = session.createQuery(hql); + query.setParameter("id", id); + List ampCategoryValues = query.list(); + if (ampCategoryValues != null && !ampCategoryValues.isEmpty()) { + return ampCategoryValues.get(0); + } + return null; + } + + protected static AmpOrganisation getAmpOrganisationById(Long id) { + Session session = PersistenceManager.getRequestDBSession(); + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = "FROM " + AmpOrganisation.class.getName() + " a " + + "WHERE a.ampOrgId = :id"; + Query query = session.createQuery(hql); + query.setParameter("id", id); + List ampOrganisations = query.list(); + if (ampOrganisations != null && !ampOrganisations.isEmpty()) { + return ampOrganisations.get(0); + } + return null; + } + + protected static AmpCurrency getAmpCurrencyById(Long id) { + Session session = PersistenceManager.getRequestDBSession(); + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = "FROM " + AmpCurrency.class.getName() + " a " + + "WHERE a.ampCurrencyId = :id"; + Query query = session.createQuery(hql); + query.setParameter("id", id); + List ampCurrencies = query.list(); + if (ampCurrencies != null && !ampCurrencies.isEmpty()) + return ampCurrencies.get(0); + return null; + } + + + public static void updateSectors(ImportDataModel importDataModel, String name, Session session, boolean primary) { + + if (ConstantsMap.containsKey("sector_" + name)) { + Long sectorId = ConstantsMap.get("sector_" + name); + logger.info("In cache... sector " + "sector_" + name + ":" + sectorId); + createSector(importDataModel, primary, sectorId); + } else { + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + + session.doWork(connection -> { + String query = primary ? "SELECT ams.amp_sector_id AS amp_sector_id, ams.name AS name FROM amp_sector ams JOIN amp_classification_config acc ON ams.amp_sec_scheme_id=acc.classification_id WHERE LOWER(ams.name) = LOWER(?) AND acc.name='Primary'" : "SELECT ams.amp_sector_id AS amp_sector_id, ams.name AS name FROM amp_sector ams JOIN amp_classification_config acc ON ams.amp_sec_scheme_id=acc.classification_id WHERE LOWER(ams.name) = LOWER(?) AND acc.name='Secondary'"; + try (PreparedStatement statement = connection.prepareStatement(query)) { + // Set the name as a parameter to the prepared statement + statement.setString(1, name); + + // Execute the query and process the results + try (ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { + Long ampSectorId = resultSet.getLong("amp_sector_id"); + createSector(importDataModel, primary, ampSectorId); + ConstantsMap.put("sector_" + name, ampSectorId); + } + } + + } catch (SQLException e) { + logger.error("Error getting sectors", e); + } + }); + } + + + } + + public static void updateLocations(ImportDataModel importDataModel, String locationName, Session session) { + logger.info("Updating locations"); + + if (ConstantsMap.containsKey("location_" + locationName)) { + Long location = ConstantsMap.get("location_" + locationName); + logger.info("In cache... location " + "location_" + locationName + ":" + location); + importDataModel.getLocations().add(new Location(location, 100.00)); + + } else { + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + + session.doWork(connection -> { + String query = "SELECT acvl.id AS location_id FROM amp_category_value_location acvl WHERE LOWER(acvl.location_name) = LOWER(?)"; + try (PreparedStatement statement = connection.prepareStatement(query)) { + statement.setString(1, locationName); + + try (ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { + Long location = resultSet.getLong("location_id"); + logger.info("Location:" + location); + importDataModel.getLocations().add(new Location(location, 100.00)); +// importDataModel.setImplementation_location(location); + ConstantsMap.put("location_" + locationName, location); + } + } + + } catch (SQLException e) { + logger.error("Error getting locations", e); + } + + }); + + + } + updateImpLevels(importDataModel,session); + + + } + + public static void updateImpLevels(ImportDataModel importDataModel, Session session) + { + if (ConstantsMap.containsKey("implementation_level_")) { + Long implementationLevel = ConstantsMap.get("implementation_level_"); + logger.info("In cache... imp level "+"implementation_level:"+implementationLevel); + importDataModel.setImplementation_level(implementationLevel); + }else { + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + + session.doWork(connection -> { + String query2 = "SELECT acv.id as implementation_level FROM amp_category_value acv JOIN amp_category_class acc ON acv.amp_category_class_id=acc.id WHERE LOWER(acv.category_value)=? AND LOWER(acc.keyname)=?"; + try (PreparedStatement statement = connection.prepareStatement(query2)) { + statement.setString(1, "national"); + statement.setString(2, "implementation_level"); + + try (ResultSet resultSet = statement.executeQuery()) { + while (resultSet.next()) { + Long implementationLevel = resultSet.getLong("implementation_level"); + logger.info("Imp level:" + implementationLevel); + importDataModel.setImplementation_level(implementationLevel); + ConstantsMap.put("implementation_level_", implementationLevel); + } + } + + } catch (SQLException e) { + logger.error("Error getting imp levels", e); + }}); + } + } + + private static void createSector(ImportDataModel importDataModel, boolean primary, Long ampSectorId) { + Sector sector1 = new Sector(); + sector1.setSector_percentage(100.00); + sector1.setSector(ampSectorId); + if (primary) { + importDataModel.getPrimary_sectors().add(sector1); + } + else + { + importDataModel.getSecondary_sectors().add(sector1); + + } + } + + private static Long getRandomOrg(Session session) + { + Long randomOrg; + if (ConstantsMap.containsKey("randomOrg")) { + randomOrg = ConstantsMap.get("randomOrg"); + logger.info("In cache... randomOrg "+randomOrg); + }else { + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = "SELECT o.ampOrgId FROM " + AmpOrganisation.class.getName() + " o"; + + randomOrg = (Long) session.createQuery(hql).setMaxResults(1).uniqueResult(); + ConstantsMap.put("randomOrg",randomOrg); + } + return randomOrg; + + + } + + public static Long updateOrgs(ImportDataModel importDataModel, String name, String code, Session session, String type) + { + Long orgId; + + if (ConstantsMap.containsKey("org_"+name+"_"+code)) { + orgId = ConstantsMap.get("org_"+name+"_"+code); + logger.info("In cache... organisation "+"org_"+name+"_"+code+":"+orgId); + } + else { + if (!session.isOpen()) { + session = PersistenceManager.getRequestDBSession(); + } + String hql = ""; + Query query; + List organisations= new ArrayList<>(); + if (name!=null) { + hql = "SELECT o.ampOrgId FROM " + AmpOrganisation.class.getName() + " o WHERE LOWER(o.name)=LOWER(:name)"; + query = session.createQuery(hql); + query.setParameter("name", name); + organisations = query.list(); + } + if (organisations.isEmpty() && (code!=null)) { + hql = "SELECT o.ampOrgId FROM " + AmpOrganisation.class.getName() + " o WHERE LOWER(o.orgCode)=LOWER(:code)"; + query = session.createQuery(hql); + query.setParameter("code", code); + organisations = query.list(); + + } + if (!organisations.isEmpty()) { + orgId = organisations.get(0); + } else { + hql = "SELECT o.ampOrgId FROM " + AmpOrganisation.class.getName() + " o"; + + query = session.createQuery(hql).setMaxResults(1); + orgId = (Long) query.uniqueResult(); + } + ConstantsMap.put("org_"+name+"_"+code, orgId); + } + logger.info("Organisation: " + orgId); + + if (Objects.equals(type, "donor")) { + DonorOrganization donorOrganization = new DonorOrganization(); + donorOrganization.setOrganization(orgId); + donorOrganization.setPercentage(100.0); + importDataModel.getDonor_organization().add(donorOrganization); + } + else if (Objects.equals(type, "responsibleOrg")) + { + Organization responsibleOrg = new Organization(); + responsibleOrg.setOrganization(orgId); + importDataModel.getResponsible_organization().add(responsibleOrg); + + } + else if (Objects.equals(type, "beneficiaryAgency")) + { + Organization beneficiaryAgency = new Organization(); + beneficiaryAgency.setOrganization(orgId); + importDataModel.getBeneficiary_agency().add(beneficiaryAgency); + + } + return orgId; + + + + } + public static int getColumnIndexByName(Sheet sheet, String columnName) { + try { + Row headerRow = sheet.getRow(0); + for (int i = 0; i < headerRow.getLastCellNum(); i++) { + Cell cell = headerRow.getCell(i); + if (cell != null && columnName.equals(cell.getStringCellValue())) { + return i; + } + } + return -1; + }catch (Exception e) + { +// logger.error("Error getting column index for "+columnName,e); + return -1; + } + + } +} diff --git a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.hbm.xml b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.hbm.xml index db6c71e2b42..60baa6514c9 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.hbm.xml +++ b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.hbm.xml @@ -20,8 +20,8 @@ - - + + diff --git a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.java b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.java index 8537352991e..9a3dddefe3c 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponent.java @@ -39,7 +39,7 @@ */ @TranslatableClass (displayName = "Component") public class AmpComponent implements Serializable, Comparable, Versionable, Cloneable, Identifiable { - + //IATI-check: to be ignored @InterchangeableId @@ -84,7 +84,7 @@ public static class AmpComponentComparator implements Comparator{ public int compare(AmpComponent o1, AmpComponent o2) { return staticCompare(o1, o2); } - + public static int staticCompare(AmpComponent o1, AmpComponent o2) { if (o1 == null) return 1; @@ -103,7 +103,7 @@ public static int staticCompare(AmpComponent o1, AmpComponent o2) { @Interchangeable(fieldTitle = COMPONENT_TYPE, importable = true, pickIdOnly = true, fmPath = "/Activity Form/Components/Component/Component Information/Component Type") private AmpComponentType type; - + private String Url; public AmpActivityVersion getActivity() { @@ -138,21 +138,21 @@ public String getTitle() { public void setTitle(String title) { this.title = title; } - + public AmpComponentType getType() { return type; } public void setType(AmpComponentType type) { this.type = type; } - + public void setUrl(String url) { Url = url; } public String getUrl() { return Url; } - + /** * A simple string comparison to sort components by title */ @@ -170,7 +170,7 @@ public boolean equals(Object obj) { AmpComponent target=(AmpComponent) obj; if (this.ampComponentId == null) return super.equals(obj); - + if (target!=null && this.ampComponentId!=null){ return (this.getAmpComponentId().equals(target.getAmpComponentId())); } @@ -182,28 +182,28 @@ public int hashCode() { return this.ampComponentId.hashCode(); } */ - + public java.sql.Timestamp getCreationdate() { return creationdate; } public void setCreationdate(java.sql.Timestamp creationdate) { this.creationdate = creationdate; } - + public Set getFundings() { return fundings; } - + public void setFundings(Set fundings) { this.fundings = fundings; } - + @Override public boolean equalsForVersioning(Object obj) { AmpComponent aux = (AmpComponent) obj; return this.getValue().equals(aux.getValue()); } - + private static final Comparator COMPONENT_FUNDING_COMPARATOR = new Comparator() { public int compare(AmpComponentFunding o1, AmpComponentFunding o2) { AmpComponentFunding aux1 = (AmpComponentFunding) o1; @@ -225,7 +225,7 @@ public int compare(AmpComponentFunding o1, AmpComponentFunding o2) { public Output getOutput() { Output out = new Output(); out.setOutputs(new ArrayList()); - + out.getOutputs().add( new Output(null, new String[] { "Title" }, new Object[] { this.title != null ? this.title : "Empty Title" })); @@ -243,15 +243,15 @@ public Output getOutput() { if (this.Url != null && !this.Url.trim().equals("")) { out.getOutputs().add(new Output(null, new String[] { "URL" }, new Object[] { this.Url })); } - + List auxFundings = new ArrayList(this.fundings); auxFundings.sort(COMPONENT_FUNDING_COMPARATOR); Iterator iter = auxFundings.iterator(); - + while(iter.hasNext()) { AmpComponentFunding funding = iter.next(); String transactionType = ""; - + switch (funding.getTransactionType().intValue()) { case 0: transactionType = "Commitments"; @@ -269,45 +269,43 @@ public Output getOutput() { transactionType = "MTEF Projection"; break; } - + out.getOutputs().add(new Output(null, new String[] { "Trn" }, new Object[] { transactionType })); out.getOutputs().add(new Output(null, new String[] { "Value" }, new Object[] { " " + funding.getAdjustmentType().getValue() + " - " , funding.getTransactionAmount(), " ", funding.getCurrency(), " - ", funding.getTransactionDate()})); } - - + + return out; } - + @Override public Object getValue() { StringBuffer ret = new StringBuffer(); ret.append("-" + this.code+ "-" + this.description + "-" + this.Url + "-" + this.creationdate); - + List auxFundings = new ArrayList(this.fundings); auxFundings.sort(COMPONENT_FUNDING_COMPARATOR); Iterator iter = auxFundings.iterator(); - + while(iter.hasNext()) { AmpComponentFunding funding = iter.next(); ret.append(funding.getTransactionType() + "-" + funding.getTransactionAmount() + "-" + funding.getCurrency() + "-" + funding.getTransactionDate()); } - + return ret.toString(); } - + @Override public Object prepareMerge(AmpActivityVersion newActivity) throws CloneNotSupportedException { AmpComponent auxComponent = (AmpComponent) clone(); auxComponent.setActivity(newActivity); auxComponent.setAmpComponentId(null); - - if (auxComponent.getFundings() != null && auxComponent.getFundings().size() > 0) { + + if (auxComponent.getFundings() != null && !auxComponent.getFundings().isEmpty()) { Set auxSetFundings = new HashSet(); - Iterator it = auxComponent.getFundings().iterator(); - while (it.hasNext()) { - AmpComponentFunding auxComponentFunding = it.next(); + for (AmpComponentFunding auxComponentFunding : auxComponent.getFundings()) { AmpComponentFunding newComponentFunding = (AmpComponentFunding) auxComponentFunding.clone(); newComponentFunding.setAmpComponentFundingId(null); newComponentFunding.setComponent(auxComponent); @@ -317,7 +315,7 @@ public Object prepareMerge(AmpActivityVersion newActivity) throws CloneNotSuppor } else { auxComponent.setFundings(null); } - + return auxComponent; } @@ -326,7 +324,7 @@ public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } - + @Override public String toString() { return title; diff --git a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponentFunding.java b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponentFunding.java index 1b93be81620..4382b6f0f59 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponentFunding.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpComponentFunding.java @@ -259,5 +259,21 @@ public void setTransactionType(Integer transactionType) { public Object clone() throws CloneNotSupportedException { return super.clone(); } - + + @Override + public String toString() { + return "AmpComponentFunding{" + + "ampComponentFundingId=" + ampComponentFundingId + + ", transactionType=" + transactionType + + ", adjustmentType=" + adjustmentType + + ", transactionDate=" + transactionDate + + ", reportingDate=" + reportingDate + + ", transactionAmount=" + transactionAmount + + ", reportingOrganization=" + reportingOrganization + + ", componentSecondResponsibleOrganization=" + componentSecondResponsibleOrganization + + ", currency=" + currency + + ", description='" + description + '\'' + + ", component=" + component + + '}'; + } } diff --git a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpFunding.java b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpFunding.java index a6a1f253c73..cbd48dcf2bf 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpFunding.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/dbentity/AmpFunding.java @@ -75,21 +75,21 @@ public class AmpFunding implements Serializable, Versionable, Cloneable, Identif private Date actualStartDate; @Interchangeable(fieldTitle="Actual Completion Date",fmPath="/Activity Form/Planning/Actual Completion Date", importable=true) private Date actualCompletionDate; - + @Interchangeable(fieldTitle = "Original Completion Date", importable = true, fmPath = "/Activity Form/Planning/Original Completion Date", interValidators = @InterchangeableValidator(value = RequiredValidator.class, groups = Submit.class, fmPath = "/Activity Form/Planning/Required Validator for Original Completion Date")) private Date originalCompDate; - + private Date lastAuditDate; @Interchangeable(fieldTitle="Reporting Date", importable=true) private Date reportingDate; - + @Interchangeable(fieldTitle="Conditions",fmPath="/Activity Form/Funding/Funding Group/Funding Item/Conditions", importable=true) @TranslatableField private String conditions; - + @Interchangeable(fieldTitle="Donor Objective",fmPath="/Activity Form/Funding/Funding Group/Funding Item/Donor Objective", importable=true) @TranslatableField private String donorObjective; @@ -98,7 +98,7 @@ public class AmpFunding implements Serializable, Versionable, Cloneable, Identif private String calType; private String comments; private Date signatureDate; - + @TransactionOrgRole(groups = API.class) @InterchangeableDiscriminator(discriminatorField = "transactionType", configurer = AmpFundingDetailDiscriminationConfigurer.class, @@ -129,11 +129,11 @@ public class AmpFunding implements Serializable, Versionable, Cloneable, Identif importable = true) }) private Set fundingDetails = new HashSet<>(); - + @Interchangeable(fieldTitle = ActivityFieldsConstants.MTEF_PROJECTIONS, importable = true, fmPath = "/Activity Form/Funding/Funding Group/Funding Item/MTEF Projections") private Set mtefProjections = new HashSet<>(); - + @Interchangeable(fieldTitle="Active",fmPath="/Activity Form/Funding/Funding Group/Funding Item/Active", importable = true) private Boolean active; @Interchangeable(fieldTitle="Delegated Cooperation",fmPath="/Activity Form/Funding/Funding Group/Funding Item/Delegated Cooperation", importable = true) @@ -142,30 +142,30 @@ public class AmpFunding implements Serializable, Versionable, Cloneable, Identif private Boolean delegatedPartner; // private AmpModality modalityId; - + @Interchangeable(fieldTitle = "Type of Assistance", - fmPath = "/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Type of Assistence", + fmPath = "/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Type of Assistence", discriminatorOption = CategoryConstants.TYPE_OF_ASSISTENCE_KEY, importable = true, pickIdOnly = true, requiredDependencies = {FundingWithTransactionsValidator.TRANSACTION_PRESENT_KEY}, dependencyRequired = ALWAYS) private AmpCategoryValue typeOfAssistance; - + @Interchangeable(fieldTitle = "Financing Instrument", - fmPath = "/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Financing Instrument", + fmPath = "/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Financing Instrument", discriminatorOption = CategoryConstants.FINANCING_INSTRUMENT_KEY, importable = true, pickIdOnly = true, requiredDependencies = {FundingWithTransactionsValidator.TRANSACTION_PRESENT_KEY}, dependencyRequired = ALWAYS) private AmpCategoryValue financingInstrument; - - @Interchangeable(fieldTitle="Funding Status", fmPath="/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Funding Status", + + @Interchangeable(fieldTitle="Funding Status", fmPath="/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Funding Status", discriminatorOption = CategoryConstants.FUNDING_STATUS_KEY, importable=true, pickIdOnly=true) private AmpCategoryValue fundingStatus; - - @Interchangeable(fieldTitle="Mode of Payment", fmPath="/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Mode of Payment", + + @Interchangeable(fieldTitle="Mode of Payment", fmPath="/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Mode of Payment", discriminatorOption = CategoryConstants.MODE_OF_PAYMENT_KEY, importable=true, pickIdOnly=true) private AmpCategoryValue modeOfPayment; - - @Interchangeable(fieldTitle="Concessionality Level", fmPath="/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Concessionality Level", + + @Interchangeable(fieldTitle="Concessionality Level", fmPath="/Activity Form/Funding/Funding Group/Funding Item/Funding Classification/Concessionality Level", discriminatorOption = CategoryConstants.CONCESSIONALITY_LEVEL_KEY, importable=true, pickIdOnly=true) private AmpCategoryValue concessionalityLevel; @@ -254,7 +254,7 @@ public class AmpFunding implements Serializable, Versionable, Cloneable, Identif private Integer orderNumber; private Integer index; - + @Override public boolean equalsForVersioning(Object obj) { AmpFunding auxFunding = (AmpFunding)obj; @@ -263,73 +263,67 @@ public boolean equalsForVersioning(Object obj) { } return false; } - + @Override public Object getValue() { // Compare fields from AmpFunding. StringBuffer ret = new StringBuffer(); - ret.append("-Type of Assistance:" + (this.typeOfAssistance != null ? this.typeOfAssistance.getEncodedValue() : "")); - ret.append("-Financing Instrument:" + (this.financingInstrument != null ? this.financingInstrument.getEncodedValue() : "")); - ret.append("-Funding classification date:" + (this.financingInstrument != null ? this.financingInstrument.getEncodedValue() : "")); - ret.append("-Conditions:" + (this.conditions == null ? "" : this.conditions.trim())); - ret.append("-Donor Objective:" + (this.donorObjective == null ? "" : this.donorObjective.trim())); - ret.append("-Active:" + this.active); - ret.append("-Delegated Cooperation:" + this.delegatedCooperation); - ret.append("-Delegated Partner:" + this.delegatedPartner); - ret.append("-Mode Of Payment:" + (this.modeOfPayment != null ? this.modeOfPayment.getEncodedValue() : "")); - ret.append("-Concessionality Level:" + (this.concessionalityLevel != null ? this.concessionalityLevel.getEncodedValue() : "")); - ret.append("-Funding Status:" + (this.fundingStatus != null ? this.fundingStatus.getEncodedValue() : "")); - ret.append("-Funding Status:" + (this.financingId != null ? this.financingId : "")); - ret.append("-Vulnerability Groups:" + (this.vulnerableGroup != null ? this.vulnerableGroup : "")); - ret.append("-Donor Objective:" + (this.donorObjective == null ? "" : this.donorObjective.trim())); - ret.append("-Projects Results Link:" + (this.projectResultsLink == null ? "" : this.projectResultsLink.trim())); + ret.append("-Type of Assistance:").append(this.typeOfAssistance != null ? this.typeOfAssistance.getEncodedValue() : ""); + ret.append("-Financing Instrument:").append(this.financingInstrument != null ? this.financingInstrument.getEncodedValue() : ""); + ret.append("-Funding classification date:").append(this.financingInstrument != null ? this.financingInstrument.getEncodedValue() : ""); + ret.append("-Conditions:").append(this.conditions == null ? "" : this.conditions.trim()); + ret.append("-Donor Objective:").append(this.donorObjective == null ? "" : this.donorObjective.trim()); + ret.append("-Active:").append(this.active); + ret.append("-Delegated Cooperation:").append(this.delegatedCooperation); + ret.append("-Delegated Partner:").append(this.delegatedPartner); + ret.append("-Mode Of Payment:").append(this.modeOfPayment != null ? this.modeOfPayment.getEncodedValue() : ""); + ret.append("-Concessionality Level:").append(this.concessionalityLevel != null ? this.concessionalityLevel.getEncodedValue() : ""); + ret.append("-Funding Status:").append(this.fundingStatus != null ? this.fundingStatus.getEncodedValue() : ""); + ret.append("-Funding Status:").append(this.financingId != null ? this.financingId : ""); + ret.append("-Vulnerability Groups:").append(this.vulnerableGroup != null ? this.vulnerableGroup : ""); + ret.append("-Donor Objective:").append(this.donorObjective == null ? "" : this.donorObjective.trim()); + ret.append("-Projects Results Link:").append(this.projectResultsLink == null ? "" : this.projectResultsLink.trim()); if (this.agreement != null) ret.append("-Agreement:" + this.agreement.getValue()); // Compare fields from AmpFundingDetail. List auxDetails = new ArrayList(this.fundingDetails); - Collections.sort(auxDetails, fundingDetailsComparator); - Iterator iter = auxDetails.iterator(); - while (iter.hasNext()) { - AmpFundingDetail auxDetail = iter.next(); - ret.append(auxDetail.getTransactionType() + "-" + auxDetail.getTransactionAmount() + "-" + auxDetail.getAmpCurrencyId() + "-" + auxDetail.getTransactionDate()); + auxDetails.sort(fundingDetailsComparator); + for (AmpFundingDetail auxDetail : auxDetails) { + ret.append(auxDetail.getTransactionType()).append("-").append(auxDetail.getTransactionAmount()).append("-").append(auxDetail.getAmpCurrencyId()).append("-").append(auxDetail.getTransactionDate()); if (auxDetail.getPledgeid() != null) ret.append(auxDetail.getPledgeid().getId()); - ret.append("-" + auxDetail.getDisbOrderId()); - if (auxDetail.getContract() != null) ret.append("-" + auxDetail.getContract().getId()); - ret.append("-" + auxDetail.getExpCategory()); - ret.append("-" + auxDetail.getDisbursementOrderRejected()); - if (auxDetail.getRecipientOrg() != null) ret.append("- recipient " + auxDetail.getRecipientOrg().getAmpOrgId() + " with role of " + auxDetail.getRecipientRole().getAmpRoleId()); + ret.append("-").append(auxDetail.getDisbOrderId()); + if (auxDetail.getContract() != null) ret.append("-").append(auxDetail.getContract().getId()); + ret.append("-").append(auxDetail.getExpCategory()); + ret.append("-").append(auxDetail.getDisbursementOrderRejected()); + if (auxDetail.getRecipientOrg() != null) + ret.append("- recipient ").append(auxDetail.getRecipientOrg().getAmpOrgId()).append(" with role of ").append(auxDetail.getRecipientRole().getAmpRoleId()); } // Compare fields from AmpFundingMTEFProjection. List auxMTEFProjection = new ArrayList(this .mtefProjections); - Collections.sort(auxMTEFProjection, fundingMTEFProjectionComparator); + auxMTEFProjection.sort(fundingMTEFProjectionComparator); for (AmpFundingMTEFProjection projection : auxMTEFProjection) { - ret.append(projection.getTransactionType() + "-" + projection.getAmount() + "-" - + projection.getAmpCurrencyId() + "-" + projection.getProjectionDate()); + ret.append(projection.getTransactionType()).append("-").append(projection.getAmount()).append("-").append(projection.getAmpCurrencyId()).append("-").append(projection.getProjectionDate()); if (projection.getAdjustmentType() != null) { - ret.append("-" + projection.getAdjustmentType().getId()); + ret.append("-").append(projection.getAdjustmentType().getId()); } } return ret.toString(); } // Compare by transaction type, then amount, then date. // (transient in order to be wicket friendly, no need to serialize this field) - private transient Comparator fundingDetailsComparator = new Comparator(){ - - - public int compare(Object o1, Object o2) { - AmpFundingDetail aux1 = (AmpFundingDetail)o1; - AmpFundingDetail aux2 = (AmpFundingDetail)o2; - if (aux1.getTransactionType().equals(aux2.getTransactionType())) { - if (aux1.getTransactionAmount().equals(aux2.getTransactionAmount())) { - return aux1.getTransactionDate().compareTo(aux2.getTransactionDate()); - } else { - return aux1.getTransactionAmount().compareTo(aux2.getTransactionAmount()); - } + private transient Comparator fundingDetailsComparator = (o1, o2) -> { + AmpFundingDetail aux1 = (AmpFundingDetail)o1; + AmpFundingDetail aux2 = (AmpFundingDetail)o2; + if (aux1.getTransactionType().equals(aux2.getTransactionType())) { + if (aux1.getTransactionAmount().equals(aux2.getTransactionAmount())) { + return aux1.getTransactionDate().compareTo(aux2.getTransactionDate()); } else { - return aux1.getTransactionType().compareTo(aux2.getTransactionType()); + return aux1.getTransactionAmount().compareTo(aux2.getTransactionAmount()); } + } else { + return aux1.getTransactionType().compareTo(aux2.getTransactionType()); } }; @@ -342,9 +336,9 @@ public int compare(Object o1, Object o2) { public Output getOutput() { Output out = new Output(); out.setOutputs(new ArrayList()); - + String orgName = this.ampDonorOrgId.getName(); - if (this.ampDonorOrgId != null + if (this.ampDonorOrgId != null && this.ampDonorOrgId.getDeleted() != null && this.ampDonorOrgId.getDeleted()) { orgName += " (" + TranslatorWorker.translateText("deleted") + ")"; out.setDeletedValues(true); @@ -404,7 +398,7 @@ public Output getOutput() { String extraValues = ""; Output auxOutDetail = null; switch (auxDetail.getTransactionType().intValue()) { - case Constants.COMMITMENT: + case Constants.COMMITMENT: transactionType = "Commitments"; if (auxDetail.getPledgeid() != null) { if (auxDetail.getPledgeid().getTitle() != null) { @@ -416,8 +410,8 @@ public Output getOutput() { trnComm = true; } break; - - case Constants.DISBURSEMENT: + + case Constants.DISBURSEMENT: transactionType = " Disbursements"; if (auxDetail.getDisbOrderId() != null && auxDetail.getDisbOrderId().trim().length() > 0) extraValues += " - " + auxDetail.getDisbOrderId(); if (auxDetail.getContract() != null) extraValues += " - " + auxDetail.getContract().getContractName(); @@ -431,8 +425,8 @@ public Output getOutput() { trnDisb = true; } break; - - case Constants.EXPENDITURE: + + case Constants.EXPENDITURE: transactionType = " Expenditures"; if (auxDetail.getExpCategory() != null && auxDetail.getExpCategory().trim().length() > 0) extraValues += " - " + auxDetail.getExpCategory(); if (!trnExp) { @@ -440,8 +434,8 @@ public Output getOutput() { trnExp = true; } break; - - case Constants.DISBURSEMENT_ORDER: + + case Constants.DISBURSEMENT_ORDER: transactionType = " Disbursement Orders"; if (auxDetail.getDisbOrderId() != null && auxDetail.getDisbOrderId().trim().length() > 0) extraValues += " - " + auxDetail.getDisbOrderId(); if (auxDetail.getContract() != null) extraValues += " - " + auxDetail.getContract().getContractName(); @@ -453,27 +447,27 @@ public Output getOutput() { trnDisbOrder = true; } break; - - case Constants.ESTIMATED_DONOR_DISBURSEMENT: + + case Constants.ESTIMATED_DONOR_DISBURSEMENT: transactionType = " Estimated Donor Disbursements"; if (!trnEDD) { out.getOutputs().add(new Output(new ArrayList(), new String[]{transactionType}, new Object[]{""})); trnEDD = true; } break; - - case Constants.RELEASE_OF_FUNDS: + + case Constants.RELEASE_OF_FUNDS: transactionType = " Release of Funds"; if (!trnRoF) { out.getOutputs().add(new Output(new ArrayList(), new String[]{transactionType}, new Object[]{""})); trnRoF = true; } break; - - default: + + default: error = true; break; - + } if (!error) { String recipientInfo = ""; @@ -496,7 +490,7 @@ public Output getOutput() { } return out; } - + @Override public Object prepareMerge(AmpActivityVersion newActivity) throws CloneNotSupportedException { AmpFunding aux = (AmpFunding)clone(); @@ -532,14 +526,14 @@ public Object prepareMerge(AmpActivityVersion newActivity) throws CloneNotSuppor } return aux; } - + @Override public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub if (groupVersionedFunding == null) groupVersionedFunding = System.currentTimeMillis(); return super.clone(); } - + /** * should the funding under this FundingItem be counted in an activity's totals * @return @@ -547,20 +541,20 @@ public Object clone() throws CloneNotSupportedException { public boolean isCountedInTotals() { return this.isDonorFunding() || this.isSscFunding(); } - + protected boolean isDonorFunding() { return (this.getSourceRole() == null) || ((this.getSourceRole() != null) && this.getSourceRole().getRoleCode().equals(Constants.ROLE_CODE_DONOR)); } - + protected boolean isSscFunding() { for (AmpFundingDetail afd : this.fundingDetails) if (afd.isSscTransaction()) return true; return false; } - + public Date getFundingClassificationDate() { return fundingClassificationDate; } - + public void setFundingClassificationDate(Date fundingClassificationDate) { this.fundingClassificationDate = fundingClassificationDate; } @@ -585,347 +579,347 @@ public void setFundingClosingDate(Date fundingClosingDate) { public Long getAmpFundingId() { return this.ampFundingId; } - + @java.lang.SuppressWarnings("all") public AmpOrganisation getAmpDonorOrgId() { return this.ampDonorOrgId; } - + @java.lang.SuppressWarnings("all") public AmpActivityVersion getAmpActivityId() { return this.ampActivityId; } - + @java.lang.SuppressWarnings("all") public Long getCrsTransactionNo() { return this.crsTransactionNo; } - + @java.lang.SuppressWarnings("all") public String getFinancingId() { return this.financingId; } - + @java.lang.SuppressWarnings("all") public String getFundingTermsCode() { return this.fundingTermsCode; } - + @java.lang.SuppressWarnings("all") public Date getPlannedStartDate() { return this.plannedStartDate; } - + @java.lang.SuppressWarnings("all") public Date getPlannedCompletionDate() { return this.plannedCompletionDate; } - + @java.lang.SuppressWarnings("all") public Date getActualStartDate() { return this.actualStartDate; } - + @java.lang.SuppressWarnings("all") public Date getActualCompletionDate() { return this.actualCompletionDate; } - + @java.lang.SuppressWarnings("all") public Date getOriginalCompDate() { return this.originalCompDate; } - + @java.lang.SuppressWarnings("all") public Date getLastAuditDate() { return this.lastAuditDate; } - + @java.lang.SuppressWarnings("all") public Date getReportingDate() { return this.reportingDate; } - + @java.lang.SuppressWarnings("all") public String getConditions() { return this.conditions; } - + @java.lang.SuppressWarnings("all") public String getDonorObjective() { return this.donorObjective; } - + @java.lang.SuppressWarnings("all") public String getLanguage() { return this.language; } - + @java.lang.SuppressWarnings("all") public String getVersion() { return this.version; } - + @java.lang.SuppressWarnings("all") public String getCalType() { return this.calType; } - + @java.lang.SuppressWarnings("all") public String getComments() { return this.comments; } - + @java.lang.SuppressWarnings("all") public Date getSignatureDate() { return this.signatureDate; } - + @java.lang.SuppressWarnings("all") public Set getFundingDetails() { return this.fundingDetails; } - + @java.lang.SuppressWarnings("all") public Set getMtefProjections() { return this.mtefProjections; } - + @java.lang.SuppressWarnings("all") public Boolean getActive() { return this.active; } - + @java.lang.SuppressWarnings("all") public Boolean getDelegatedCooperation() { return this.delegatedCooperation; } - + @java.lang.SuppressWarnings("all") public Boolean getDelegatedPartner() { return this.delegatedPartner; } - + @java.lang.SuppressWarnings("all") public AmpCategoryValue getTypeOfAssistance() { return this.typeOfAssistance; } - + @java.lang.SuppressWarnings("all") public AmpCategoryValue getFinancingInstrument() { return this.financingInstrument; } - + @java.lang.SuppressWarnings("all") public AmpCategoryValue getFundingStatus() { return this.fundingStatus; } - + @java.lang.SuppressWarnings("all") public AmpCategoryValue getModeOfPayment() { return this.modeOfPayment; } - + @java.lang.SuppressWarnings("all") public String getLoanTerms() { return this.loanTerms; } - + @java.lang.SuppressWarnings("all") public Long getGroupVersionedFunding() { return this.groupVersionedFunding; } - + @java.lang.SuppressWarnings("all") public Float getCapitalSpendingPercentage() { return this.capitalSpendingPercentage; } - + @java.lang.SuppressWarnings("all") public AmpAgreement getAgreement() { return this.agreement; } - + @java.lang.SuppressWarnings("all") public AmpRole getSourceRole() { return this.sourceRole; } - + @java.lang.SuppressWarnings("all") public Comparator getFundingDetailsComparator() { return this.fundingDetailsComparator; } - + @java.lang.SuppressWarnings("all") public void setAmpFundingId(final Long ampFundingId) { this.ampFundingId = ampFundingId; } - + @java.lang.SuppressWarnings("all") public void setAmpDonorOrgId(final AmpOrganisation ampDonorOrgId) { this.ampDonorOrgId = ampDonorOrgId; } - + @java.lang.SuppressWarnings("all") public void setAmpActivityId(final AmpActivityVersion ampActivityId) { this.ampActivityId = ampActivityId; } - + @java.lang.SuppressWarnings("all") public void setCrsTransactionNo(final Long crsTransactionNo) { this.crsTransactionNo = crsTransactionNo; } - + @java.lang.SuppressWarnings("all") public void setFinancingId(final String financingId) { this.financingId = financingId; } - + @java.lang.SuppressWarnings("all") public void setFundingTermsCode(final String fundingTermsCode) { this.fundingTermsCode = fundingTermsCode; } - + @java.lang.SuppressWarnings("all") public void setPlannedStartDate(final Date plannedStartDate) { this.plannedStartDate = plannedStartDate; } - + @java.lang.SuppressWarnings("all") public void setPlannedCompletionDate(final Date plannedCompletionDate) { this.plannedCompletionDate = plannedCompletionDate; } - + @java.lang.SuppressWarnings("all") public void setActualStartDate(final Date actualStartDate) { this.actualStartDate = actualStartDate; } - + @java.lang.SuppressWarnings("all") public void setActualCompletionDate(final Date actualCompletionDate) { this.actualCompletionDate = actualCompletionDate; } - + @java.lang.SuppressWarnings("all") public void setOriginalCompDate(final Date originalCompDate) { this.originalCompDate = originalCompDate; } - + @java.lang.SuppressWarnings("all") public void setLastAuditDate(final Date lastAuditDate) { this.lastAuditDate = lastAuditDate; } - + @java.lang.SuppressWarnings("all") public void setReportingDate(final Date reportingDate) { this.reportingDate = reportingDate; } - + @java.lang.SuppressWarnings("all") public void setConditions(final String conditions) { this.conditions = conditions; } - + @java.lang.SuppressWarnings("all") public void setDonorObjective(final String donorObjective) { this.donorObjective = donorObjective; } - + @java.lang.SuppressWarnings("all") public void setLanguage(final String language) { this.language = language; } - + @java.lang.SuppressWarnings("all") public void setVersion(final String version) { this.version = version; } - + @java.lang.SuppressWarnings("all") public void setCalType(final String calType) { this.calType = calType; } - + @java.lang.SuppressWarnings("all") public void setComments(final String comments) { this.comments = comments; } - + @java.lang.SuppressWarnings("all") public void setSignatureDate(final Date signatureDate) { this.signatureDate = signatureDate; } - + @java.lang.SuppressWarnings("all") public void setFundingDetails(final Set fundingDetails) { this.fundingDetails = fundingDetails; } - + @java.lang.SuppressWarnings("all") public void setMtefProjections(final Set mtefProjections) { this.mtefProjections = mtefProjections; } - + @java.lang.SuppressWarnings("all") public void setActive(final Boolean active) { this.active = active; } - + @java.lang.SuppressWarnings("all") public void setDelegatedCooperation(final Boolean delegatedCooperation) { this.delegatedCooperation = delegatedCooperation; } - + @java.lang.SuppressWarnings("all") public void setDelegatedPartner(final Boolean delegatedPartner) { this.delegatedPartner = delegatedPartner; } - + @java.lang.SuppressWarnings("all") public void setTypeOfAssistance(final AmpCategoryValue typeOfAssistance) { this.typeOfAssistance = typeOfAssistance; } - + @java.lang.SuppressWarnings("all") public void setFinancingInstrument(final AmpCategoryValue financingInstrument) { this.financingInstrument = financingInstrument; } - + @java.lang.SuppressWarnings("all") public void setFundingStatus(final AmpCategoryValue fundingStatus) { this.fundingStatus = fundingStatus; } - + @java.lang.SuppressWarnings("all") public void setModeOfPayment(final AmpCategoryValue modeOfPayment) { this.modeOfPayment = modeOfPayment; } - + @java.lang.SuppressWarnings("all") public void setLoanTerms(final String loanTerms) { this.loanTerms = loanTerms; } - + @java.lang.SuppressWarnings("all") public void setGroupVersionedFunding(final Long groupVersionedFunding) { this.groupVersionedFunding = groupVersionedFunding; } - + @java.lang.SuppressWarnings("all") public void setCapitalSpendingPercentage(final Float capitalSpendingPercentage) { this.capitalSpendingPercentage = capitalSpendingPercentage; } - + @java.lang.SuppressWarnings("all") public void setAgreement(final AmpAgreement agreement) { this.agreement = agreement; } - + @java.lang.SuppressWarnings("all") public void setSourceRole(final AmpRole sourceRole) { this.sourceRole = sourceRole; } - + @java.lang.SuppressWarnings("all") public void setFundingDetailsComparator(final Comparator fundingDetailsComparator) { this.fundingDetailsComparator = fundingDetailsComparator; @@ -978,11 +972,11 @@ public Integer getIndex() { public void setIndex(Integer index) { this.index = index; } - + public void setConcessionalityLevel(AmpCategoryValue concessionalityLevel) { this.concessionalityLevel = concessionalityLevel; } - + public AmpCategoryValue getConcessionalityLevel() { return this.concessionalityLevel; } diff --git a/amp/WEB-INF/src/org/digijava/module/aim/form/DataImporterForm.java b/amp/WEB-INF/src/org/digijava/module/aim/form/DataImporterForm.java index 419cdd9d064..7b96f0ca0f3 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/form/DataImporterForm.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/form/DataImporterForm.java @@ -10,6 +10,9 @@ public class DataImporterForm extends ActionForm { List fieldInfos =new ArrayList<>(); private FormFile dataFile; + + private boolean internal; + public Set getFileHeaders() { return fileHeaders; } @@ -65,7 +68,13 @@ public List getFieldInfos() { public void setFieldInfos(List fieldInfos) { this.fieldInfos = fieldInfos; } + public boolean isInternal() { + return internal; + } + public void setInternal(boolean internal) { + this.internal = internal; + } @Override public String toString() { return "DataImporterForm{" + diff --git a/amp/WEB-INF/src/org/digijava/module/aim/form/ImportProgressForm.java b/amp/WEB-INF/src/org/digijava/module/aim/form/ImportProgressForm.java index e2fc12071af..3f759831be2 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/form/ImportProgressForm.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/form/ImportProgressForm.java @@ -1,8 +1,8 @@ package org.digijava.module.aim.form; import org.apache.struts.action.ActionForm; -import org.digijava.module.admin.dbentity.ImportedFilesRecord; -import org.digijava.module.admin.dbentity.ImportedProject; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedFilesRecord; +import org.digijava.module.aim.action.dataimporter.dbentity.ImportedProject; import java.util.ArrayList; import java.util.List; diff --git a/amp/WEB-INF/src/org/digijava/module/aim/util/ActivityUtil.java b/amp/WEB-INF/src/org/digijava/module/aim/util/ActivityUtil.java index c670e7f9681..6931a1413ec 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/util/ActivityUtil.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/util/ActivityUtil.java @@ -112,7 +112,7 @@ public class ActivityUtil { public static List getComponents(Long actId) { Session session = null; - List col = new ArrayList(); + List col = new ArrayList<>(); logger.info(" this is the other components getting called...."); try { session = PersistenceManager.getRequestDBSession(); @@ -598,7 +598,7 @@ public static AmpOrganisation getAmpOrganisation(Long actId, Long orgRoleId) { AmpOrganisation organisation = null; try { session = PersistenceManager.getSession(); - String rewrittenColumns = SQLUtils.rewriteQuery("amp_organisation", "ao", + String rewrittenColumns = SQLUtils.rewriteQuery("amp_organisation", "ao", new HashMap(){{ put("name", InternationalizedModelDescription.getForProperty(AmpOrganisation.class, "name").getSQLFunctionCall("ao.amp_org_id")); put("description", InternationalizedModelDescription.getForProperty(AmpOrganisation.class, "description").getSQLFunctionCall("ao.amp_org_id")); @@ -637,63 +637,57 @@ public static Collection getAllComponents(Long id) { try { session = PersistenceManager.getSession(); AmpActivityVersion activity = (AmpActivityVersion) session.load(AmpActivityVersion.class, id); - Set comp = activity.getComponents(); - if (comp != null && comp.size() > 0) { - Iterator itr1 = comp.iterator(); - while (itr1.hasNext()) { - AmpComponent ampComp = (AmpComponent) itr1.next(); - Components components = new Components(); - components.setComponentId(ampComp.getAmpComponentId()); - components.setDescription(ampComp.getDescription()); - components.setType_Id((ampComp.getType()!=null)?ampComp.getType().getType_id():null); - components.setTitle(ampComp.getTitle()); - components.setCommitments(new ArrayList()); - components.setDisbursements(new ArrayList()); - components.setExpenditures(new ArrayList()); - - Collection componentsFunding = ampComp.getFundings(); - Iterator compFundIterator = componentsFunding.iterator(); - while (compFundIterator.hasNext()) { - AmpComponentFunding cf = (AmpComponentFunding) compFundIterator.next(); - FundingDetail fd = new FundingDetail(); - fd.setAdjustmentTypeName(cf.getAdjustmentType()); - - fd.setCurrencyCode(cf.getCurrency().getCurrencyCode()); - fd.setCurrencyName(cf.getCurrency().getCurrencyName()); - fd.setTransactionAmount(FormatHelper.formatNumber(cf.getTransactionAmount().doubleValue())); - fd.setTransactionDate(DateConversion.convertDateToLocalizedString(cf.getTransactionDate())); - fd.setFiscalYear(DateConversion.convertDateToFiscalYearString(cf.getTransactionDate())); - fd.setTransactionType(cf.getTransactionType().intValue()); - if (fd.getTransactionType() == Constants.COMMITMENT) { - components.getCommitments().add(fd); - } - else if (fd.getTransactionType() == Constants.DISBURSEMENT) { - components.getDisbursements().add(fd); - } - else if (fd.getTransactionType() == Constants.EXPENDITURE) { - components.getExpenditures().add(fd); - } - } - List list = null; - if (components.getCommitments() != null) { - list = new ArrayList(components.getCommitments()); - Collections.sort(list, FundingValidator.dateComp); - } - components.setCommitments(list); - list = null; - if (components.getDisbursements() != null) { - list = new ArrayList(components.getDisbursements()); - Collections.sort(list, FundingValidator.dateComp); - } - components.setDisbursements(list); - list = null; - if (components.getExpenditures() != null) { - list = new ArrayList(components.getExpenditures()); - Collections.sort(list, FundingValidator.dateComp); + Set comp = activity.getComponents(); + if (comp != null && !comp.isEmpty()) { + for (AmpComponent o : comp) { + Components components = new Components<>(); + components.setComponentId(o.getAmpComponentId()); + components.setDescription(o.getDescription()); + components.setType_Id((o.getType() != null) ? o.getType().getType_id() : null); + components.setTitle(o.getTitle()); + components.setCommitments(new ArrayList<>()); + components.setDisbursements(new ArrayList<>()); + components.setExpenditures(new ArrayList<>()); + + Collection componentsFunding = o.getFundings(); + for (AmpComponentFunding cf : componentsFunding) { + FundingDetail fd = new FundingDetail(); + fd.setAdjustmentTypeName(cf.getAdjustmentType()); + + fd.setCurrencyCode(cf.getCurrency().getCurrencyCode()); + fd.setCurrencyName(cf.getCurrency().getCurrencyName()); + fd.setTransactionAmount(FormatHelper.formatNumber(cf.getTransactionAmount().doubleValue())); + fd.setTransactionDate(DateConversion.convertDateToLocalizedString(cf.getTransactionDate())); + fd.setFiscalYear(DateConversion.convertDateToFiscalYearString(cf.getTransactionDate())); + fd.setTransactionType(cf.getTransactionType()); + if (fd.getTransactionType() == Constants.COMMITMENT) { + components.getCommitments().add(fd); + } else if (fd.getTransactionType() == Constants.DISBURSEMENT) { + components.getDisbursements().add(fd); + } else if (fd.getTransactionType() == Constants.EXPENDITURE) { + components.getExpenditures().add(fd); + } + } + List list = null; + if (components.getCommitments() != null) { + list = new ArrayList<>(components.getCommitments()); + Collections.sort(list, FundingValidator.dateComp); + } + components.setCommitments(list); + list = null; + if (components.getDisbursements() != null) { + list = new ArrayList(components.getDisbursements()); + Collections.sort(list, FundingValidator.dateComp); + } + components.setDisbursements(list); + list = null; + if (components.getExpenditures() != null) { + list = new ArrayList(components.getExpenditures()); + Collections.sort(list, FundingValidator.dateComp); + } + components.setExpenditures(list); + componentsCollection.add(components); } - components.setExpenditures(list); - componentsCollection.add(components); - } } } @@ -716,7 +710,7 @@ public static Collection getFundingComponentActivity(Long c Query qry = PersistenceManager.getSession().createQuery(qryStr); return qry.list(); } - + public static Collection getOldActivities(Session session,int size,Date date){ List colAv; Collection colAll = new ArrayList(); @@ -731,7 +725,7 @@ public static Collection getOldActivities(Session session,in BigInteger id = (BigInteger) objects[0]; idActivities.add(id.longValue()); } - if(idActivities.size()>0){ + if(!idActivities.isEmpty()){ String qryGroups = "select av from " + AmpActivityVersion.class.getName()+" av where av.ampActivityId in:list"; Query qry = session.createQuery(qryGroups); @@ -752,10 +746,10 @@ public static Collection getOldActivities(Session session,in // function for getting fundings for components and ids ends here - - + + /** - * checks whether the 'name' activity title exists as a translation in any language + * checks whether the 'name' activity title exists as a translation in any language * in any latest version of an activity * excluding the activity group of the current activity * @param name title of the activity @@ -800,18 +794,18 @@ public void execute(Connection conn) throws SQLException { if (result.getId() == -1l) return null; return result; - + } - + public static List getSortedActivitiesByDonors (List acts, boolean acs) { List retVal = new ArrayList(); Map donorNameActivityMap = new HashMap (); List noFundingActivities = null; for (AmpActivityVersion actItem : acts) { - if (actItem.getFunding() != null && !actItem.getFunding().isEmpty()) { + if (actItem.getFunding() != null && !actItem.getFunding().isEmpty()) { StringBuilder donorNames = new StringBuilder(); - + List organizations = new ArrayList<>(actItem.getFunding()); if (organizations != null && organizations.size() > 1) { Collections.sort(organizations, new Comparator() { @@ -835,14 +829,14 @@ public int compare(AmpFunding o1, AmpFunding o2) { noFundingActivities.add(actItem); } } - + Set keys = donorNameActivityMap.keySet(); List sortedKeys = new ArrayList (keys); Collections.sort(sortedKeys); if (!acs) { Collections.reverse(sortedKeys); } - + for (String key : sortedKeys) { retVal.add(donorNameActivityMap.get(key)); } @@ -850,16 +844,16 @@ public int compare(AmpFunding o1, AmpFunding o2) { if (noFundingActivities != null) { retVal.addAll(noFundingActivities); } - + return retVal; } /* * get the the Contracts for Activity - * + * */ public static List getIPAContracts(Long activityId) { - + String queryString = "select con from " + IPAContract.class.getName() + " con " + "where (con.activity=:activityId)"; Query qry = PersistenceManager.getSession().createQuery(queryString).setLong("activityId",activityId ); @@ -878,21 +872,21 @@ public static List getIPAContracts(Long activityId) { c.setTotalDisbursements(new Double(td)); c.setExecutionRate(ActivityUtil.computeExecutionRateFromTotalAmount(c, c.getTotalAmountCurrency().getCurrencyCode())); c.setFundingTotalDisbursements(ActivityUtil.computeFundingDisbursementIPA(c, cc)); - c.setFundingExecutionRate(ActivityUtil.computeExecutionRateFromContractTotalValue(c, cc)); + c.setFundingExecutionRate(ActivityUtil.computeExecutionRateFromContractTotalValue(c, cc)); } return contrcats ; - } + } public static double computeFundingDisbursementIPA(IPAContract contract, String cc){ - - ArrayList disbs1 = (ArrayList) DbUtil.getDisbursementsFundingOfIPAContract(contract); + + ArrayList disbs1 = (ArrayList) DbUtil.getDisbursementsFundingOfIPAContract(contract); //if there is no disbursement global currency saved in db we'll use the default from edit activity form - + if(contract.getTotalAmountCurrency()!=null) cc=contract.getTotalAmountCurrency().getCurrencyCode(); double td=0; - double usdAmount=0; - double finalAmount=0; + double usdAmount=0; + double finalAmount=0; for(Iterator j=disbs1.iterator();j.hasNext();) { @@ -919,12 +913,12 @@ public static double computeFundingDisbursementIPA(IPAContract contract, String // contract.setFundingExecutionRate(ActivityUtil.computeExecutionRateFromContractTotalValue(contract, cc)); return td; } - + public static double computeExecutionRateFromContractTotalValue(IPAContract c, String currCode){ - double usdAmount1=0; - double finalAmount1=0; + double usdAmount1=0; + double finalAmount1=0; try { - if(c.getContractTotalValue()!=null && c.getTotalAmountCurrency().getCurrencyCode()!=null) + if(c.getContractTotalValue()!=null && c.getTotalAmountCurrency().getCurrencyCode()!=null) usdAmount1 = CurrencyWorker.convertToUSD(c.getContractTotalValue().doubleValue(),c.getTotalAmountCurrency().getCurrencyCode()); else usdAmount1 = 0.0; } catch (AimException e) { @@ -936,8 +930,8 @@ public static double computeExecutionRateFromContractTotalValue(IPAContract c, S } catch (AimException e) { // TODO Auto-generated catch block e.printStackTrace(); - } - + } + double execRate=0; if(finalAmount1!=0) execRate=c.getFundingTotalDisbursements()/finalAmount1; @@ -946,10 +940,10 @@ public static double computeExecutionRateFromContractTotalValue(IPAContract c, S } public static double computeExecutionRateFromTotalAmount(IPAContract c, String currCode){ - double usdAmount1=0; - double finalAmount1=0; + double usdAmount1=0; + double finalAmount1=0; try { - if(c.getTotalAmount()!=null && c.getTotalAmountCurrency()!=null ) + if(c.getTotalAmount()!=null && c.getTotalAmountCurrency()!=null ) usdAmount1 = CurrencyWorker.convertToUSD(c.getTotalAmount().doubleValue(),c.getTotalAmountCurrency().getCurrencyCode()); else usdAmount1=0.0; } catch (AimException e) { @@ -959,8 +953,8 @@ public static double computeExecutionRateFromTotalAmount(IPAContract c, String c finalAmount1 = CurrencyWorker.convertFromUSD(usdAmount1,currCode); } catch (AimException e) { e.printStackTrace(); - } - + } + double execRate=0; if(finalAmount1!=0) execRate=c.getTotalDisbursements()/finalAmount1; @@ -976,7 +970,7 @@ public static Set fetchLongs(final String usedQuery) { Set ampActivityIds = new TreeSet(); List res; - + if (usedQuery.contains(":")) { // slower but always works res = PersistenceManager.getSession().doReturningWork(new ReturningWork>() { @@ -994,7 +988,7 @@ public static Set fetchLongs(final String usedQuery) } return ampActivityIds; } - + /** * returns a set of all ampActivityIds passed by the workspace filter * @param session @@ -1002,10 +996,10 @@ public static Set fetchLongs(final String usedQuery) public static Set getAllLegalAmpActivityIds() { return fetchLongs(WorkspaceFilter.getWorkspaceFilterQuery(TLSUtils.getRequest().getSession())); } - + public static List getLastUpdatedActivities() { String workspaceQuery = Util.toCSStringForIN(getAllLegalAmpActivityIds()); - + List res = new ArrayList(); Session session = null; Query qry = null; @@ -1028,7 +1022,7 @@ public static List getLastUpdatedActivities() { } return res; } - + /* * this function is so incredibly slow that you should NEVER use it. Left it here because it is used by one very old (most probably unused) page */ @@ -1036,12 +1030,12 @@ public static List getAllActivitiesList() { String queryString = "select ampAct from " + AmpActivityVersion.class.getName() + " ampAct"; return PersistenceManager.getSession().createQuery(queryString).list(); } - + public static List getActivitiesWhichMatchDate(String dateField, Date value) { Date minDate = new Date(value.getTime() - 24 * 3600l * 1000l); Date maxDate = new Date(value.getTime() + 24 * 3600l * 1000l); String queryString = String.format( - "select ampAct from %s ampAct WHERE (ampAct.team IS NOT NULL) AND " + + "select ampAct from %s ampAct WHERE (ampAct.team IS NOT NULL) AND " + "(ampAct.%s >= :minDate) AND (ampAct.%s <= :maxDate)", AmpActivityVersion.class.getName(), dateField, dateField); List aavs = PersistenceManager.getSession() @@ -1093,7 +1087,7 @@ public static List getAllActivitiesByName(String name) { .setParameter("name", "%" + name + "%", StringType.INSTANCE) .list(); } - + private static AmpActivityGroup getActivityGroups(Session session, Long actId) { String queryString = "select group from " + AmpActivityGroup.class.getName() + " group " + "where group.ampActivityLastVersion.ampActivityId=:actId"; @@ -1128,7 +1122,7 @@ public static void deleteActivityContent(AmpActivityVersion ampAct, Session sess String deleteActivityTeam = "DELETE FROM amp_team_activities WHERE amp_activity_id = " + ampAct.getAmpActivityId(); SQLUtils.executeQuery(con, deleteActivityTeam ); } - + public static void removeMergeSources(Long ampActivityId,Session session){ String queryString1 = "select act from " + AmpActivityVersion.class.getName() + " act where (act.mergeSource1=:activityId)"; String queryString2 = "select act from " + AmpActivityVersion.class.getName() + " act where (act.mergeSource2=:activityId)"; @@ -1136,7 +1130,7 @@ public static void removeMergeSources(Long ampActivityId,Session session){ Query qry2 = session.createQuery(queryString2); qry1.setParameter("activityId", ampActivityId, LongType.INSTANCE); qry2.setParameter("activityId", ampActivityId, LongType.INSTANCE); - + Collection col =qry1.list(); if (col != null && col.size() > 0) { Iterator itrAmp = col.iterator(); @@ -1155,9 +1149,9 @@ public static void removeMergeSources(Long ampActivityId,Session session){ session.update(actVersion); } } - + } - + /** * @deprecated * @@ -1183,7 +1177,7 @@ public static List getActivities(Set ampActIds){ .setCacheable(false).list(); } } - + public static void deleteActivityIndicatorsSession(Long ampActivityId,Session session) throws Exception{ Collection col = null; Query qry = null; @@ -1193,17 +1187,17 @@ public static void deleteActivityIndicatorsSession(Long ampActivityId,Session se qry = session.createQuery(queryString); qry.setParameter("ampActId", ampActivityId, LongType.INSTANCE); col = qry.list(); - + Iterator itrIndAct = col.iterator(); while(itrIndAct.hasNext()){ IndicatorActivity indAct =(IndicatorActivity)itrIndAct.next(); session.delete(indAct); } - + } public static void deleteActivityIndicators(Collection activityInd, AmpActivityVersion activity, Session session) throws Exception { - + if (activityInd != null && activityInd.size() > 0) { for (Object indAct : activityInd) { @@ -1370,13 +1364,13 @@ public static List getActivityProgramsByProgramType(Long act " ap join ap.programSetting s where (ap.activity=:actId) and (s.name=:settingName)"; return PersistenceManager.getSession().createQuery(queryString).setLong("actId",actId).setString("settingName",settingName).list(); } - + public static class HelperAmpActivityNameComparator implements Comparator { public int compare(Object obj1, Object obj2) { AmpActivityVersion act1 = (AmpActivityVersion) obj1; AmpActivityVersion act2 = (AmpActivityVersion) obj2; - return (act1.getName()!=null && act2.getName()!=null)?act1.getName().compareTo(act2.getName()):0; + return (act1.getName()!=null && act2.getName()!=null)?act1.getName().compareTo(act2.getName()):0; } } @@ -1386,7 +1380,7 @@ public int compare(Object obj1, Object obj2) { * @param user,actId * @return ampId * @author dare - * @param session + * @param session */ public static String generateAmpId(User user, Long actId, Session session) { String globSetting = "numeric";// TODO This should come from global settings @@ -1396,26 +1390,26 @@ public static String generateAmpId(User user, Long actId, Session session) { else return combinedAmpId(actId); } - + /** * combines countryId, current member id and last activityId+1 and makes ampId * @param user,actId - * @return + * @return * @author dare - * @param session + * @param session */ private static String numericAmpId(User user, Long actId, Session session){ String countryCode = FeaturesUtil.getGlobalSettingValue(org.digijava.module.aim.helper.Constants.GLOBAL_DEFAULT_COUNTRY); String userId = user.getId().toString(); - Country country = (Country) session.load(Country.class, countryCode); + Country country = (Country) session.load(Country.class, countryCode); String countryId = "0"; if (country != null){ countryId = country.getCountryId().toString(); } - + String lastId = null; if (actId != null){ - lastId = actId.toString(); + lastId = actId.toString(); } return countryId + userId + lastId; } @@ -1423,7 +1417,7 @@ private static String numericAmpId(User user, Long actId, Session session){ /** * combines countryIso and last activityId+1 and makes ampId * @param actId - * @return + * @return * @author dare */ private static String combinedAmpId(Long actId){ @@ -1431,18 +1425,18 @@ private static String combinedAmpId(Long actId){ String countryCode = FeaturesUtil.getGlobalSettingValue(org.digijava.module.aim.helper.Constants.GLOBAL_DEFAULT_COUNTRY); String lastId = null; if (actId != null){ - lastId = actId.toString(); - } - retVal = countryCode.toUpperCase() + "/" + lastId; + lastId = actId.toString(); + } + retVal = countryCode.toUpperCase() + "/" + lastId; return retVal; } - + public static List getActivitiesRelatedToAmpTeamMember(Session session, Long ampTeamMemberId) { String queryStr = "SELECT a FROM " + AmpActivityVersion.class.getName() + " a left join a.member m WHERE " + "(a.activityCreator=:atmId) OR (a.modifiedBy=:atmId) OR (a.approvedBy = :atmId) OR (m.ampTeamMemId = :atmId) OR (a.modifiedBy = :atmId)"; return session.createQuery(queryStr).setLong("atmId", ampTeamMemberId).list(); } - + public static String collectionToCSV(Collection activities) { if (activities == null) return null; @@ -1454,13 +1448,13 @@ public static String collectionToCSV(Collection activities) else ret += "' '" + ", "; } - return ret.substring(0, ret.length() - 2); + return ret.substring(0, ret.length() - 2); } - + /** * @author Dare * @param partOfName - * @return Array of Strings,which have a look like: activity_name(activity_id) + * @return Array of Strings,which have a look like: activity_name(activity_id) */ public static String[] loadActivitiesNamesAndIds(TeamMember member) throws DgException{ Session session=null; @@ -1470,7 +1464,7 @@ public static String[] loadActivitiesNamesAndIds(TeamMember member) throws DgExc String [] retValue=null; try { session=PersistenceManager.getRequestDBSession(); - + Set relatedTeams=TeamUtil.getRelatedTeamsForMember(member); Set teamAO = TeamUtil.getComputedOrgs(relatedTeams); String activityNameString = AmpActivityVersion.hqlStringForName("a"); @@ -1479,7 +1473,7 @@ public static String[] loadActivitiesNamesAndIds(TeamMember member) throws DgExc queryString = "select " + activityNameString + ", a.ampActivityId from " + AmpActivity.class.getName() + " a left outer join a.orgrole r left outer join a.funding f " + " where a.team in (" + Util.toCSStringForIN(relatedTeams) + ") or (r.organisation in (" + Util.toCSStringForIN(teamAO) + ") or f.ampDonorOrgId in (" + Util.toCSStringForIN(teamAO) + ")) order by " + activityNameString; - } else + } else { // not computed (e.g. team) workspace queryString = "select " + activityNameString + ", a.ampActivityId from " + AmpActivity.class.getName() + " a where a.team in (" + Util.toCSString(relatedTeams) + ") "; @@ -1487,20 +1481,20 @@ public static String[] loadActivitiesNamesAndIds(TeamMember member) throws DgExc // queryString += " and approvalStatus in (" + Util.toCSString(activityStatus) + ") "; // } queryString += " order by " + activityNameString; - } - - query=session.createQuery(queryString); - activities=query.list(); - }catch(Exception ex) { - logger.error("couldn't load Activities" + ex.getMessage()); - ex.printStackTrace(); - } + } + + query=session.createQuery(queryString); + activities=query.list(); + }catch(Exception ex) { + logger.error("couldn't load Activities" + ex.getMessage()); + ex.printStackTrace(); + } if (activities != null){ - retValue=new String[activities.size()]; + retValue=new String[activities.size()]; int i=0; for (Object rawRow : activities) { Object[] row = (Object[])rawRow; //:) - String nameRow=(String)row[0]; + String nameRow=(String)row[0]; if(nameRow != null){ nameRow = nameRow.replace('\n', ' '); nameRow = nameRow.replace('\r', ' '); @@ -1508,7 +1502,7 @@ public static String[] loadActivitiesNamesAndIds(TeamMember member) throws DgExc } ////System.out.println(nameRow); retValue[i]=nameRow+"("+row[1]+")"; - i++; + i++; } } return retValue; @@ -1527,12 +1521,12 @@ public static String[] searchActivitiesNamesAndIds(TeamMember member, String sea Set teamAO = TeamUtil.getComputedOrgs(relatedTeams); String activityName = AmpActivityVersion.hqlStringForName("gr.ampActivityLastVersion"); - queryString ="select " + activityName + ", gr.ampActivityLastVersion.ampActivityId from "+ AmpActivityGroup.class.getName()+" gr "; + queryString ="select " + activityName + ", gr.ampActivityLastVersion.ampActivityId from "+ AmpActivityGroup.class.getName()+" gr "; if (teamAO != null && !teamAO.isEmpty()) { queryString +=" left outer join gr.ampActivityLastVersion.orgrole r left outer join gr.ampActivityLastVersion.funding f "+ " where gr.ampActivityLastVersion.team in (" + Util.toCSStringForIN(relatedTeams) + ") " + " or (r.organisation in (" + Util.toCSStringForIN(teamAO) + ") or f.ampDonorOrgId in (" + Util.toCSStringForIN(teamAO) + ")) "; - + } else { // none computed workspace queryString += " where gr.ampActivityLastVersion.team in (" @@ -1564,22 +1558,22 @@ public static String[] searchActivitiesNamesAndIds(TeamMember member, String sea } return retValue; } - - /** + + /** * @param actId * @return activity name * @author dare */ public static String getActivityName(Long actId){ String activityName = AmpActivityVersion.hqlStringForName("gr.ampActivityLastVersion"); - String queryString = "select " + activityName + " from "+ AmpActivityGroup.class.getName()+" gr where gr.ampActivityLastVersion.ampActivityId = " + actId; - return PersistenceManager.getSession().createQuery(queryString).uniqueResult().toString(); + String queryString = "select " + activityName + " from "+ AmpActivityGroup.class.getName()+" gr where gr.ampActivityLastVersion.ampActivityId = " + actId; + return PersistenceManager.getSession().createQuery(queryString).uniqueResult().toString(); } - + /** * @author Marcelo - * @param - * @return Array of Strings, which have budget_code_project_id's + * @param + * @return Array of Strings, which have budget_code_project_id's */ public static String[] getBudgetCodes() throws DgException{ Session session=null; @@ -1589,18 +1583,18 @@ public static String[] getBudgetCodes() throws DgException{ String [] retValue=null; try { session=PersistenceManager.getRequestDBSession(); - queryString = "select distinct a.budgetCodeProjectID from " + AmpActivityVersion.class.getName() + " a"; - query=session.createQuery(queryString); - activities=query.list(); - }catch(Exception ex) { - logger.error("couldn't load Activities" + ex.getMessage()); - ex.printStackTrace(); - } + queryString = "select distinct a.budgetCodeProjectID from " + AmpActivityVersion.class.getName() + " a"; + query=session.createQuery(queryString); + activities=query.list(); + }catch(Exception ex) { + logger.error("couldn't load Activities" + ex.getMessage()); + ex.printStackTrace(); + } if (activities != null){ - //filtering null and blank values + //filtering null and blank values ArrayList codes = new ArrayList(); for (Object rawRow : activities) { - String val = (String)rawRow; + String val = (String)rawRow; if(val!=null && val.trim().compareTo("")!=0){ codes.add(val); } @@ -1617,7 +1611,7 @@ public static String[] getBudgetCodes() throws DgException{ } return retValue; } - + public static ArrayList getAllActivitiesAdmin(String searchTerm, Set frozenActivityIds, ActivityForm.DataFreezeFilter dataFreezeFilter) { try { Session session = PersistenceManager.getSession(); @@ -1626,7 +1620,7 @@ public static ArrayList getAllActivitiesAdmin(String searchTerm && (frozenActivityIds == null || frozenActivityIds.isEmpty())) { return new ArrayList<>(); } - + boolean isSearchByName = searchTerm != null && (!searchTerm.trim().isEmpty()); String activityName = AmpActivityVersion.hqlStringForName("f"); @@ -1657,7 +1651,7 @@ public static ArrayList getAllActivitiesAdmin(String searchTerm + "FROM " + AmpActivity.class.getName() + " as f left join f.team as ampTeam left join f.ampActivityGroup as ampGroup WHERE " + nameSearchQuery + " ((f.deleted = false) or (f.deleted is null))" + dataFreezeQuery; - + Query qry = session.createQuery(queryString); if (isSearchByName) { qry.setString("searchTerm", "%" + searchTerm + "%"); @@ -1705,21 +1699,21 @@ public static void deleteAmpActivityWithVersions(Long ampActId) throws Exception } session.delete(ampActivityGroup); } - + public static void deleteFullActivityContent(AmpActivityVersion ampAct, Session session) throws Exception{ ActivityUtil.deleteActivityContent(ampAct,session); Long ampActId = ampAct.getAmpActivityId(); //This is not deleting AmpMEIndicators, just indicators, ME is deprecated. ActivityUtil.deleteActivityIndicators(DbUtil.getActivityMEIndValue(ampActId), ampAct, session); } - + public static void deleteAllActivityContent(AmpActivityVersion ampAct, Session session) throws Exception{ ActivityUtil.deleteActivityContent(ampAct,session); Long ampActId = ampAct.getAmpActivityId(); ActivityUtil.removeMergeSources(ampActId, session); ActivityUtil.deleteActivityIndicatorsSession(ampActId, session); } - + public static Integer activityExists (Long versionId,Session session) throws Exception{ Integer retVal = null; try { @@ -1727,11 +1721,11 @@ public static Integer activityExists (Long versionId,Session session) throws Exc retVal = (Integer)qry.uniqueResult(); } catch (Exception e) { e.printStackTrace(); - } - return retVal; + } + return retVal; } - - + + /** * returns a subset of activities which can/should be validated by a team member * @param tm @@ -1748,7 +1742,7 @@ public static Set getActivitiesWhichShouldBeValidated(TeamMember tm, Colle "AND (a.approval_status = 'started' OR a.approval_status='edited' OR a.approval_status='rejected') AND (a.draft IS NULL OR a.draft IS FALSE)"; // AND (a.amp_team_id = " + tm.getTeamId() + ")"; if (!crossTeamValidationEnabled) query += " AND (a.amp_team_id = " + tm.getTeamId() + ")"; - + List validated_activity_ids = PersistenceManager.getSession().createSQLQuery(query).list(); for(BigInteger bi:validated_activity_ids) result.add(bi.longValue()); @@ -1760,10 +1754,10 @@ public static Set getActivitiesWhichShouldBeValidated(TeamMember tm, Colle } } - + public static ArrayList getIssues(Long actId) { ArrayList col = new ArrayList<>(); - + AmpActivityVersion activity = null; try { Session session = PersistenceManager.getRequestDBSession(); @@ -1779,11 +1773,11 @@ public static ArrayList getIssues(Long a } - + public static void changeActivityArchiveStatus(Collection activityIds, boolean status) { try { Session session = PersistenceManager.getRequestDBSession(); - String qryString = "update " + AmpActivityVersion.class.getName() + + String qryString = "update " + AmpActivityVersion.class.getName() + " av set av.archived=:archived where av.ampActivityId in (" + Util.toCSStringForIN(activityIds) + ")"; Query query = session.createQuery(qryString); query.setBoolean("archived", status); @@ -1793,8 +1787,8 @@ public static void changeActivityArchiveStatus(Collection activityIds, boo catch (Exception e) { e.printStackTrace(); } - } - + } + public static AmpStructureImg getStructureImage(Long structureId, Long imgId) { return DbUtil.getStructureImage(structureId, imgId); } @@ -1843,7 +1837,7 @@ public static Period getProjectImplementationDelay(AmpActivityVersion activity) Date toDate; if (fromDate == null) return null; - + if (activity.getActualCompletionDate() != null) toDate = activity.getActualCompletionDate(); else if (activity.getProposedCompletionDate() != null) @@ -1853,7 +1847,7 @@ else if (activity.getProposedCompletionDate() != null) return DateConversion.getPeriod(fromDate, toDate); return null; } - + /** Get the user first name and last name who modified (created) the activity. * @param actitivity * @param auditHistory @@ -1863,7 +1857,7 @@ public static String getModifiedByUserName(AmpActivityVersion actitivity, Activi AmpTeamMember modifiedBy = actitivity.getModifiedBy(); AmpTeamMember createdBy = actitivity.getActivityCreator(); AmpTeamMember approvedBy = actitivity.getApprovedBy(); - + if (modifiedBy != null) { return String.format("%s %s", modifiedBy.getUser().getFirstNames(), modifiedBy.getUser().getLastName()); } else if(auditHistory != null) { @@ -1873,10 +1867,10 @@ public static String getModifiedByUserName(AmpActivityVersion actitivity, Activi } else if (createdBy != null) { return String.format("%s %s", createdBy.getUser().getFirstNames(), createdBy.getUser().getLastName()); } - + return ""; } - + /** Get modified date * @param activity * @param auditHistory @@ -1893,10 +1887,10 @@ public static Date getModifiedByDate(AmpActivityVersion activity, ActivityHistor } else if (activity.getCreatedDate() != null) { return activity.getCreatedDate(); } - + return null; } - + /** * Get audit info about the activity from amp_audit_logger table * @param activityId @@ -1905,7 +1899,7 @@ public static Date getModifiedByDate(AmpActivityVersion activity, ActivityHistor public static ActivityHistory getModifiedByInfoFromAuditLogger(Long activityId) { ActivityHistory logActivityHistory = new ActivityHistory(); List activityLogObjects = AuditLoggerUtil.getActivityLogObjects(activityId.toString()); - + for(AmpAuditLogger aal : activityLogObjects) { if (StringUtils.isNotEmpty(aal.getEditorName())) { logActivityHistory.setModifiedBy(aal.getEditorName()); @@ -1920,7 +1914,7 @@ public static ActivityHistory getModifiedByInfoFromAuditLogger(Long activityId) } } } - + return null; } @@ -2061,13 +2055,13 @@ public static List getEditableActivityIds(TeamMember tm, String query) { public static boolean canValidateActivity(AmpActivityVersion activity, TeamMember teamMember) { boolean canValidate = false; - + if (!activity.getDraft()) { AmpApplicationSettings appSettings = AmpARFilter.getEffectiveSettings(); String validationOption = appSettings != null ? appSettings.getValidation() : null; - + boolean isTeamMemberValidator = isTeamMemberValidator(teamMember, activity); - + if (isTeamMemberValidator) { if (Constants.PROJECT_VALIDATION_FOR_ALL_EDITS.equalsIgnoreCase(validationOption)) { if (activity.getTeam() != null @@ -2084,24 +2078,24 @@ public static boolean canValidateActivity(AmpActivityVersion activity, TeamMembe } } } - + return canValidate; } - + public static boolean isTeamMemberValidator(TeamMember teamMember, AmpActivityVersion activity) { - + if (teamMember.getTeamHead()) { return true; } - + AmpApplicationSettings appSettings = AmpARFilter.getEffectiveSettings(); - + boolean crossTeamValidation = (appSettings != null && appSettings.getTeam() != null) ? appSettings.getTeam().getCrossteamvalidation() : false; - + //Check if cross team validation is enable boolean crossTeamCheck = false; - + if (activity.getTeam() != null) { if (crossTeamValidation) { crossTeamCheck = true; @@ -2111,10 +2105,10 @@ public static boolean isTeamMemberValidator(TeamMember teamMember, AmpActivityVe crossTeamCheck = teamMember.getTeamId().equals(activity.getTeam().getAmpTeamId()); } } - + return teamMember.isApprover() && crossTeamCheck; } - + return false; } diff --git a/amp/WEB-INF/src/org/digijava/module/aim/util/TeamMemberUtil.java b/amp/WEB-INF/src/org/digijava/module/aim/util/TeamMemberUtil.java index 772996db191..14b70c84e4d 100644 --- a/amp/WEB-INF/src/org/digijava/module/aim/util/TeamMemberUtil.java +++ b/amp/WEB-INF/src/org/digijava/module/aim/util/TeamMemberUtil.java @@ -1229,7 +1229,7 @@ public static AmpTeamMember getCurrentAmpTeamMember(HttpServletRequest request) return currentAmpTeamMember; } - public static List getAmpTeamMembersbyDgUserId(Long userId) throws Exception { + public static List getAmpTeamMembersbyDgUserId(Long userId) { Session session = null; Query qry = null; diff --git a/amp/WEB-INF/src/org/digijava/module/contentrepository/util/DocumentManagerUtil.java b/amp/WEB-INF/src/org/digijava/module/contentrepository/util/DocumentManagerUtil.java index 34932e3e580..16b002eeb24 100644 --- a/amp/WEB-INF/src/org/digijava/module/contentrepository/util/DocumentManagerUtil.java +++ b/amp/WEB-INF/src/org/digijava/module/contentrepository/util/DocumentManagerUtil.java @@ -69,9 +69,9 @@ public class DocumentManagerUtil { - + private static Logger logger = Logger.getLogger(DocumentManagerUtil.class); - + /** * to be called after every HttpServletRequest processed * @param request @@ -79,7 +79,7 @@ public class DocumentManagerUtil { public static void logoutJcrSessions(HttpServletRequest request) { ContentRepositoryManager.closeSessions(request); } - + public static Session getReadSession(HttpServletRequest request) { return ContentRepositoryManager.getReadSession(request); } @@ -87,13 +87,13 @@ public static Session getReadSession(HttpServletRequest request) { public static Session getWriteSession(HttpServletRequest request) { return ContentRepositoryManager.getWriteSession(request); } - + public static NodeWrapper getReadNodeWrapper(String uuid, HttpServletRequest request) { Node n = getReadNode(uuid, request); if (n != null) { return new NodeWrapper(n); } - + return null; } @@ -101,9 +101,9 @@ public static boolean isVersionable(Node node){ try{ return node.getVersionHistory().getNodes().getSize() > 0; } - catch(Exception e){return false;} + catch(Exception e){return false;} } - + public static Node getReadNode(String uuid, HttpServletRequest request) { Session session = ContentRepositoryManager.getReadSession(request); try { @@ -123,25 +123,25 @@ public static Node getWriteNode(String uuid, HttpServletRequest request) { } } - + public static String getApplicationPath() { PathHelper ph = new DocumentManagerUtil().new PathHelper(); return ph.getApplicationPath(); } - + public static String calendarToString(Calendar cal,boolean yearofPublication) { String retVal=null; // String [] monthNames = {"", "January", "February", "March", "April", "May", "June", // "July", "August", "September", "October", "November", "December"}; - + int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); - + // int hour = cal.get(Calendar.HOUR_OF_DAY); // int minute = cal.get(Calendar.MINUTE); // int second = cal.get(Calendar.SECOND); - + if(yearofPublication){ retVal= new Long(year).toString() ; }else{ @@ -149,28 +149,28 @@ public static String calendarToString(Calendar cal,boolean yearofPublication) { } return retVal ; } - + public static Node getNodeOfLastVersion(String currentUUID, HttpServletRequest request) throws CrException, RepositoryException { List versions = getVersions(currentUUID, request, true); - - if (versions == null || versions.size() == 0) + + if (versions == null || versions.size() == 0) throw new NoVersionsFoundException("No versions were found for node with UUID: " + currentUUID); - + Version lastVersion = versions.get( versions.size()-1 ); - + NodeIterator niter = lastVersion.getNodes(); - + if ( !niter.hasNext() ) { throw new NoNodeInVersionNodeException("The last version node of node with UUID " + currentUUID + " doesn't contain any nodes"); } - - return niter.nextNode(); + + return niter.nextNode(); } public static Node getLastVersionNotWaitingApproval(String currentUUID, HttpServletRequest request) throws CrException, RepositoryException { List versions = getVersions(currentUUID, request, true); - if (versions == null || versions.size() == 0) + if (versions == null || versions.size() == 0) throw new NoVersionsFoundException("No versions were found for node with UUID: " + currentUUID); - + for ( int i=versions.size()-1; i>=0; i-- ) { Version v = versions.get(i); NodeIterator nIter = v.getNodes(); @@ -182,11 +182,11 @@ public static Node getLastVersionNotWaitingApproval(String currentUUID, HttpServ } } return null; - - - + + + } - + public static int getNextVersionNumber(String uuid, HttpServletRequest request) { List versions = getVersions(uuid, request, false); return versions.size() + 1; @@ -207,12 +207,12 @@ public static List getVersions(String uuid, HttpServletRequest request, //String uuidBaseVersion = baseVersion.getUUID(); VersionIterator iterator = history.getAllVersions(); iterator.skip(1); - + while(iterator.hasNext()) { versions.add( iterator.nextVersion() ); } return versions; - } + } catch (Exception e) { // TODO Auto-generated catch block logger.warn( e.getMessage() ); @@ -222,7 +222,7 @@ public static List getVersions(String uuid, HttpServletRequest request, } return null; } - + public static Boolean deleteDocumentWithRightsChecking (String uuid, HttpServletRequest request) throws Exception { Boolean hasDeleteRights = DocumentManagerRights.hasDeleteRights(uuid, request); if (hasDeleteRights == null) @@ -233,7 +233,7 @@ public static Boolean deleteDocumentWithRightsChecking (String uuid, HttpServlet else return new Boolean(false); } - + /** * deleted a node from JR repo * @param session - the session OR null. If null, the current TLS request's session will be used @@ -244,11 +244,11 @@ public static boolean deleteNode(Session session, String uuid) { if (uuid == null) { return false; } - + if (session == null) { session = getWriteSession(TLSUtils.getRequest()); } - + boolean successfully = false; String name = ""; try { @@ -257,7 +257,7 @@ public static boolean deleteNode(Session session, String uuid) { node.remove(); session.save(); successfully = true; - + return true; } catch (Exception e) { logger.error(uuid, e); @@ -265,14 +265,14 @@ public static boolean deleteNode(Session session, String uuid) { finally{ logger.info(String.format("DELETED JackRabbit node with uuid = %s, name = %s, success: %b", uuid, name, successfully)); } - + return false; } - + private static boolean deleteDocument(String uuid, HttpServletRequest request) { return deleteNode(getWriteSession(request), uuid); } - + public static Property getPropertyFromNode(Node n, String propertyName) { try { return n.getProperty(propertyName); @@ -285,23 +285,23 @@ public static Property getPropertyFromNode(Node n, String propertyName) { } return null; } - + public static TeamInformationBeanDM getTeamInformationBeanDM (HttpSession session) { TeamInformationBeanDM teamInfo = new TeamInformationBeanDM(); teamInfo.setMeTeamMember( (TeamMember)session.getAttribute(Constants.CURRENT_MEMBER) ); - + TeamMember me = teamInfo.getMeTeamMember(); - + if (me != null) { teamInfo.setIsTeamLeader(me.getTeamHead()); teamInfo.setMyTeamMembers(TeamMemberUtil.getAllTeamMembers(me.getTeamId())); } - + return teamInfo; } - + public static Collection createDocumentDataCollectionForActivityPreview(HttpServletRequest request) { - Collection uuids = SelectDocumentDM.getSelectedDocsSet(request, + Collection uuids = SelectDocumentDM.getSelectedDocsSet(request, ActivityDocumentsConstants.RELATED_DOCUMENTS, false); ArrayList ret = new ArrayList(); if (uuids == null) { @@ -335,21 +335,21 @@ public static Collection createDocumentDataCollectionForActivityPr if ( fileName == null && nodeWrapper.getWebLink() == null ){ continue; } - - DocumentData documentData = DocumentData.buildFromNodeWrapper(nodeWrapper, documentNodeBaseVersionUUID, - nodeWrapper.getUuid()); + + DocumentData documentData = DocumentData.buildFromNodeWrapper(nodeWrapper, documentNodeBaseVersionUUID, + nodeWrapper.getUuid()); ret.add(documentData); - } + } return ret; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } - + } - - + + public static List createDocumentDataCollectionFromSession(HttpServletRequest request) { List uuids = new ArrayList<>(); uuids.addAll(SelectDocumentDM.getSelectedDocsSet(request, ActivityDocumentsConstants.RELATED_DOCUMENTS, true)); @@ -363,37 +363,37 @@ public static List createDocumentDataCollectionFromSession(HttpSer throw new RuntimeException(e.getMessage(), e); } } - + return null; } - + public static boolean checkFileSize(FormFile formFile, ActionMessages errors) { long maxFileSizeInBytes = Long.MAX_VALUE; long maxFileSizeInMBytes = Long.MAX_VALUE; String maxFileSizeGS = FeaturesUtil.getGlobalSettingValue(GlobalSettingsConstants.CR_MAX_FILE_SIZE); // File size in MB if (maxFileSizeGS != null) { maxFileSizeInMBytes = Integer.parseInt( maxFileSizeGS ); - maxFileSizeInBytes = 1024 * 1024 * maxFileSizeInMBytes; + maxFileSizeInBytes = 1024 * 1024 * maxFileSizeInMBytes; } - + if (formFile.getFileSize() > maxFileSizeInBytes) { errors.add("title", new ActionMessage("error.contentrepository.addFile.fileTooLarge", maxFileSizeInMBytes)); - + return false; } - + if (formFile.getFileSize() < 1) { errors.add("title", new ActionMessage("error.contentrepository.addFile.badPath")); - + return false; } - + return true; } - + public static boolean checkFileContentType(FormFile formFile, ActionMessages errors) { boolean contentValid = true; - + if (ResourceManagerSettingsUtil.isLimitFileToUpload()) { try { FileTypeManager mimeTypeManager = FileTypeManager.getInstance(); @@ -402,12 +402,12 @@ public static boolean checkFileContentType(FormFile formFile, ActionMessages err if (validationResponse.getStatus() != FileTypeValidationStatus.ALLOWED) { if (validationResponse.getStatus() == FileTypeValidationStatus.NOT_ALLOWED) { errors.add("title", - new ActionMessage("error.contentrepository.addFile.contentNotAllowed", + new ActionMessage("error.contentrepository.addFile.contentNotAllowed", validationResponse.getDescription(), validationResponse.getContentName())); } else if (validationResponse.getStatus() == FileTypeValidationStatus.CONTENT_EXTENSION_MISMATCH) { - errors.add("title", - new ActionMessage("error.contentrepository.addFile.contentExtensionMismatch", - formFile.getFileName(), + errors.add("title", + new ActionMessage("error.contentrepository.addFile.contentExtensionMismatch", + formFile.getFileName(), validationResponse.getDescription() + " (" + validationResponse.getContentName() + ")")); } else { errors.add("title", new ActionMessage("error.contentrepository.addFile.internalError")); @@ -420,31 +420,31 @@ public static boolean checkFileContentType(FormFile formFile, ActionMessages err contentValid = false; } } - + return contentValid; } - + public static boolean validateFile(FormFile formFile, ActionMessages errors) { boolean hasValidSize = checkFileSize(formFile, errors); boolean hasValidContentType = checkFileContentType(formFile, errors); - + return hasValidSize && hasValidContentType; } - + public static boolean checkStringAsNodeTitle (String string) { for (int i=0; i objsUsingDoc = query.list(); - + + Collection objsUsingDoc = query.list(); + if ( objsUsingDoc != null && objsUsingDoc.size() > 0) { number = objsUsingDoc.size(); - Iterator iter = objsUsingDoc.iterator(); + Iterator iter = objsUsingDoc.iterator(); while( iter.hasNext() ) { ObjectReferringDocument obj = iter.next(); obj.remove(session); - } + } } session.flush(); - + } - - + + catch (Exception ex) { logger.error("Exception : " + ex.getMessage()); ex.printStackTrace(System.out); } return number; } - + public static String processUrl (String urlString, DocumentManagerForm formBean) { try { URL url = new URL( urlString ); @@ -493,65 +493,65 @@ public static String processUrl (String urlString, DocumentManagerForm formBean) } catch (MalformedURLException e) { if ( !urlString.startsWith("http://") ) return processUrl("http://"+urlString, formBean); - + if(formBean!=null){ formBean.addError("error.contentrepository.addFile.malformedWebLink", "Error adding new document. Web link is malformed."); - } + } e.printStackTrace(); return null; } } - + public static double bytesToMega (long bytes) { double size = ((double)bytes) / (1024*1024); int temp = (int)(size * 1000); size = ( (double)temp ) / 1000; - + return size; } - + public static Node getTeamNode(Session jcrWriteSession, Long teamId) { return DocumentManagerUtil.getNodeByPath(jcrWriteSession, "team/" + teamId); } - + public static Node getOrCreateTeamNode(Session jcrWriteSession, Long teamId) { Node teamNode = getTeamNode(jcrWriteSession, teamId); - + if (teamNode == null) { return createTeamNode(jcrWriteSession, teamId); } - + return teamNode; } - + public static Node createTeamNode(Session jcrWriteSession, Long teamId) { return DocumentManagerUtil.createNodeUsingPath(jcrWriteSession, "team/" + teamId); } - + public static Node getUserPrivateNode(Session jcrSession, TeamMember teamMember) { String userName = teamMember.getEmail(); String teamId = "" + teamMember.getTeamId(); return DocumentManagerUtil.getNodeByPath(jcrSession, "private/" + teamId + "/" + userName); } - + public static Node getOrCreateUserPrivateNode(Session jcrWriteSession, TeamMember teamMember) { Node userHomeNode = getUserPrivateNode(jcrWriteSession, teamMember); - + if (userHomeNode == null) { return createUserPrivateNode(jcrWriteSession, teamMember); } - + return userHomeNode; } - + public static Node createUserPrivateNode(Session jcrSession, TeamMember teamMember) { String userEmail = teamMember.getEmail(); String path = "private/" + teamMember.getTeamId() + "/" + userEmail; - + return DocumentManagerUtil.createNodeUsingPath(jcrSession, path); } - + public static String getWebLinkByUuid(String uuid, HttpServletRequest request) { if ( uuid==null || request==null ) return null; @@ -559,9 +559,9 @@ public static String getWebLinkByUuid(String uuid, HttpServletRequest request) { String ret = nw.getWebLink(); return ret; } - + /** - * + * * @param jcrWriteSession * @param path */ @@ -577,13 +577,13 @@ public static Node getNodeByPath(Session jcrWriteSession, String path) { return null; } } - + return folderPathNode; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } - + public static Node createNodeUsingPath(Session session, String path) { boolean toSave = false; try { @@ -599,23 +599,23 @@ public static Node createNodeUsingPath(Session session, String path) { toSave = true; } } - + if (toSave) { session.save(); } - + return folderPathNode; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } - + public class PathHelper { private String applicationPath; - + public PathHelper() { - - + + URL rootUrl = this.getClass().getResource("/"); try { String path; @@ -646,13 +646,13 @@ public PathHelper() { return; } } - + public String getApplicationPath() { return applicationPath; - } + } } - - + + /** * get node uuids which were shared or requested to be shared for the given team * @param teamId @@ -671,16 +671,16 @@ public static List getSharedNodeUUIDs(TeamMember team, Integer state) { }else{ queryString="select r.sharedNodeVersionUUID from " + CrSharedDoc.class.getName() + " r where r.team="+teamId+" and r.state="+state; } - + qry=session.createQuery(queryString); return qry.list(); } catch (Exception e) { logger.error("Couldn't Load Resourcess: " + e.toString()); } - + return Collections.emptyList(); } - + public static CrSharedDoc getCrSharedDoc(String uuid,Long teamId, Integer state){ CrSharedDoc retVal=null; org.hibernate.Session session=null; @@ -697,11 +697,11 @@ public static CrSharedDoc getCrSharedDoc(String uuid,Long teamId, Integer state) qry=session.createQuery(queryString); retVal=(CrSharedDoc)qry.uniqueResult(); } catch (Exception e) { - logger.error("Couldn't Load Resourcess: " + e.toString()); + logger.error("Couldn't Load Resourcess: " + e); } return retVal; } - + /** * searches if given node was shared on any level and if found returns them * uuid can ne base node uuid or shared version uuid @@ -721,9 +721,9 @@ public static List getSharedDocsForGivenNodeUUID(String uuid){ } return retVal; } - + /** - * + * * @param nodeUUID * @return version number of the node, which was requested to be share, if such exists. */ @@ -735,15 +735,15 @@ public static List isPrivateResourceShared(String nodeUUID){ session=PersistenceManager.getRequestDBSession(); String queryString="select r.sharedNodeVersionUUID from " + CrSharedDoc.class.getName() + " r where "; queryString+= " (r.sharedPrivateNodeUUID='"+nodeUUID+"' and r.state="+CrConstants.SHARED_IN_WORKSPACE+")"; - + qry=session.createQuery(queryString); retVal=qry.list(); } catch (Exception e) { logger.error("Couldn't Load Resources: " + e.toString()); - } + } return retVal; } - + /** * checks whether team node's some version is shared with given workspace,if found returns it's shared version */ @@ -752,10 +752,10 @@ public static String isTeamResourceSharedWithGivenWorkspace(String nodeUUID,Long String retVal=null; if(sharedRecord!=null){ retVal=sharedRecord.getSharedNodeVersionUUID(); - } + } return retVal; } - + /** * checks whether team node's any version is shared with given workspace and if found, returns it */ @@ -777,17 +777,17 @@ public static CrSharedDoc loadTeamResourceSharedWithWorkspace(String nodeUUID,Lo retVal=(CrSharedDoc)qry.uniqueResult(); } catch (Exception e) { logger.error("Couldn't Load Resources: " + e.toString()); - } + } return retVal; } - + public static boolean isGivenVersionShared(String versionUUID){ boolean retVal=false; org.hibernate.Session session=null; Query qry=null; try { session=PersistenceManager.getRequestDBSession(); - String queryString="select count(r.sharedNodeVersionUUID) from " + CrSharedDoc.class.getName() + + String queryString="select count(r.sharedNodeVersionUUID) from " + CrSharedDoc.class.getName() + " r where r.sharedNodeVersionUUID='"+versionUUID+"' and r.state!="+CrConstants.PENDING_STATUS; qry=session.createQuery(queryString); int amount=(Integer)qry.uniqueResult(); @@ -799,7 +799,7 @@ public static boolean isGivenVersionShared(String versionUUID){ } return retVal; } - + /** * checks whether resource(given version) was requested to be shared and is still not approved by TL * @param nodeUUID @@ -822,7 +822,7 @@ public static boolean isResourcePendingtoBeShared (String nodeUUID){ } return retVal; } - + public static boolean isResourceVersionPendingtoBeShared (String nodeUUID, String versionUUID){ boolean retVal=false; org.hibernate.Session session=null; @@ -844,7 +844,7 @@ public static boolean isResourceVersionPendingtoBeShared (String nodeUUID, Strin } return retVal; } - + public static NodeLastApprovedVersion getlastApprovedVersionOfTeamNode(String nodeUUID){ NodeLastApprovedVersion retVal=null; @@ -852,45 +852,45 @@ public static NodeLastApprovedVersion getlastApprovedVersionOfTeamNode(String no Query qry=null; try { session=PersistenceManager.getRequestDBSession(); - String queryString="select r from "+NodeLastApprovedVersion.class.getName() +" r where r.nodeUUID='"+nodeUUID+"'"; - qry=session.createQuery(queryString); + String queryString="select r from "+NodeLastApprovedVersion.class.getName() +" r where r.nodeUUID='"+nodeUUID+"'"; + qry=session.createQuery(queryString); retVal=(NodeLastApprovedVersion)qry.uniqueResult(); } catch (Exception e) { logger.error("Couldn't Load Last Approved Version : " + e.toString()); - } + } return retVal; } - + public static Map getLastApprovedVersionsByUUIDMap() { Map versionsMap = new HashMap<>(); for (NodeLastApprovedVersion version : getLastApprovedVersions()) { versionsMap.put(version.getNodeUUID(), version); } - + return versionsMap; } - + public static List getLastApprovedVersions() { return PersistenceManager.getRequestDBSession() .createCriteria(NodeLastApprovedVersion.class) .list(); } - + public static List getPendingVersionsForResource(String nodeUUID){ List retVal=null; org.hibernate.Session session=null; Query qry=null; try { session=PersistenceManager.getRequestDBSession(); - String queryString="select r from "+TeamNodePendingVersion.class.getName() +" r where r.nodeUUID='"+nodeUUID+"'"; - qry=session.createQuery(queryString); + String queryString="select r from "+TeamNodePendingVersion.class.getName() +" r where r.nodeUUID='"+nodeUUID+"'"; + qry=session.createQuery(queryString); retVal=qry.list(); } catch (Exception e) { logger.error("Couldn't Load Last Approved Version : " + e.toString()); - } + } return retVal; } - + /** * check whether node's versions is pending approval and if true, return this record * @param versionID @@ -902,15 +902,15 @@ public static TeamNodePendingVersion isGivenVersionPendingApproval(String versio Query qry=null; try { session=PersistenceManager.getRequestDBSession(); - String queryString="select r from "+TeamNodePendingVersion.class.getName() +" r where r.versionID='"+versionID+"'"; - qry=session.createQuery(queryString); + String queryString="select r from "+TeamNodePendingVersion.class.getName() +" r where r.versionID='"+versionID+"'"; + qry=session.createQuery(queryString); retVal=(TeamNodePendingVersion)qry.uniqueResult(); } catch (Exception e) { logger.error("Couldn't Load Last Approved Version : " + e.toString()); - } + } return retVal; } - + /** * check whether the first version of this resource was shared from private space(Used for Team Level Share and Not Across Wokrspaces) * if found, return the record @@ -921,15 +921,15 @@ public static CrSharedDoc isTeamResourceSharedFromPrivateSpace(String nodeUUID){ Query qry=null; try { session=PersistenceManager.getRequestDBSession(); - String queryString="select r from " + CrSharedDoc.class.getName() + " r where r.nodeUUID='"+nodeUUID+"' and r.state="+CrConstants.SHARED_IN_WORKSPACE; + String queryString="select r from " + CrSharedDoc.class.getName() + " r where r.nodeUUID='"+nodeUUID+"' and r.state="+CrConstants.SHARED_IN_WORKSPACE; qry=session.createQuery(queryString); retVal=(CrSharedDoc)qry.uniqueResult(); } catch (Exception e) { logger.error("Couldn't Load Resources: " + e.toString()); - } + } return retVal; } - + /** * get's it's unapproved and last approved versions if found any * @param nodeUUID @@ -941,15 +941,15 @@ public static List getTeamNodeState(String nodeUUID){ Query qry=null; try { session=PersistenceManager.getRequestDBSession(); - String queryString="select r from "+TeamNodeState.class.getName() +" r where r.nodeUUID='"+nodeUUID+"'"; - qry=session.createQuery(queryString); + String queryString="select r from "+TeamNodeState.class.getName() +" r where r.nodeUUID='"+nodeUUID+"'"; + qry=session.createQuery(queryString); retVal=qry.list(); } catch (Exception e) { logger.error("Couldn't Load Team Node States : " + e.toString()); - } + } return retVal; } - + public static void deleteNodeStates(String nodeUUID){ org.hibernate.Session session=null; Query query; @@ -961,10 +961,10 @@ public static void deleteNodeStates(String nodeUUID){ query.executeUpdate(); } catch (Exception e) { logger.error("Delete Failed: " +e.toString()); - } - } + } + } } - + public static void deleteAllShareRecordsrelatedToResource(String nodeUUID){ org.hibernate.Session session=null; Query query; @@ -976,23 +976,23 @@ public static void deleteAllShareRecordsrelatedToResource(String nodeUUID){ query.executeUpdate(); } catch (Exception e) { logger.error("Delete Failed: " +e.toString()); - } + } } } - + public static void deleteTeamNodePendingVersion(String nodeUUID, String versionUUID){ org.hibernate.Session session=null; Query qry=null; try { session=PersistenceManager.getRequestDBSession(); - String queryString="delete from "+TeamNodePendingVersion.class.getName() +" r where r.nodeUUID='"+nodeUUID+"' and r.versionID='"+versionUUID+"'"; + String queryString="delete from "+TeamNodePendingVersion.class.getName() +" r where r.nodeUUID='"+nodeUUID+"' and r.versionID='"+versionUUID+"'"; qry=session.createQuery(queryString); qry.executeUpdate(); } catch (Exception e) { logger.error("Delete Failed: " + e.toString()); } } - + public synchronized static void shutdownRepository( ServletContext sContext ) { logger.info("Shutting down jackrabbit repository"); JackrabbitRepository repository = (JackrabbitRepository)sContext.getAttribute( CrConstants.JACKRABBIT_REPOSITORY ); @@ -1002,12 +1002,12 @@ public synchronized static void shutdownRepository( ServletContext sContext ) { repository.shutdown(); logger.info("Jackrabbit repository shutdown succesfully !"); } - + public static boolean privateDocumentsExist(Session session, TeamMember teamMember) { String userName = teamMember.getEmail(); String teamId = "" + teamMember.getTeamId(); Node node = DocumentManagerUtil.getNodeByPath(session, "private/" + teamId + "/" + userName); - + if (node != null) { try { return node.hasNodes(); @@ -1019,7 +1019,7 @@ public static boolean privateDocumentsExist(Session session, TeamMember teamMemb return false; } - + public static boolean teamDocumentsExist(Session session, TeamMember teamMember) { Node node = DocumentManagerUtil.getNodeByPath(session, "team/" + teamMember.getTeamId()); if (node != null) { @@ -1032,8 +1032,8 @@ public static boolean teamDocumentsExist(Session session, TeamMember teamMember) return false; } - - + + public static boolean sharedDocumentsExist(TeamMember teamMember){ boolean retVal=false; org.hibernate.Session session=null; @@ -1049,9 +1049,9 @@ public static boolean sharedDocumentsExist(TeamMember teamMember){ } return retVal; } - + public static boolean publicDocumentsExist(TeamMember teamMember){ - boolean retVal=false; + boolean retVal=false; org.hibernate.Session session=null; Query qry=null; String queryString=null; @@ -1073,7 +1073,7 @@ public static void setMaxFileSizeAttribute(HttpServletRequest request) { request.setAttribute("uploadMaxFileSize", Long.toString(Bytes.megabytes(Long.parseLong(maxFileSizeGS)).bytes())); } - + public static ArrayList retrieveTemporaryDocDataList(HttpServletRequest request) { HashMap map = SelectDocumentDM.getContentRepositoryHashMap(request); ArrayList list = (ArrayList) map @@ -1082,7 +1082,7 @@ public static ArrayList retrieveTemporaryDocDataList(HttpServletRe list = new ArrayList(); map.put(ActivityDocumentsConstants.TEMPORARY_DOCUMENTS, list); } - + return list; } } diff --git a/amp/deployConfigs/selected.properties b/amp/deployConfigs/selected.properties index c9e4a92ccf1..16a303528e2 100644 --- a/amp/deployConfigs/selected.properties +++ b/amp/deployConfigs/selected.properties @@ -1,4 +1,4 @@ #Description of the selected config -#Wed, 13 Sep 2023 13:20:42 +0300 +#Wed, 05 Jun 2024 23:08:45 +0300 serverName=local diff --git a/amp/pom.xml b/amp/pom.xml index 8883d3ee5f4..e0ee3798b99 100644 --- a/amp/pom.xml +++ b/amp/pom.xml @@ -222,6 +222,13 @@ dom4j 1.6.1 + + + com.opencsv + opencsv + 5.5.2 + + net.sf.ehcache ehcache-core diff --git a/amp/repository/aim/module-config.xml b/amp/repository/aim/module-config.xml index 1e353866729..a88c5cd5ed5 100644 --- a/amp/repository/aim/module-config.xml +++ b/amp/repository/aim/module-config.xml @@ -29,7 +29,7 @@ ADMIN ADMIN ADMIN - ADMIN + ADMIN ADMIN ADMIN ADMIN @@ -68,7 +68,7 @@ - + @@ -116,7 +116,7 @@ org.digijava.module.aim.dbentity.AmpReports org.digijava.module.aim.dbentity.AmpReportLog org.digijava.module.aim.dbentity.AmpSectorScheme - org.digijava.module.aim.dbentity.AmpTeamReports + org.digijava.module.aim.dbentity.AmpTeamReports org.digijava.module.aim.dbentity.AmpComponentType org.digijava.module.aim.dbentity.AmpComponent org.digijava.module.aim.dbentity.AmpComponentsIndicators @@ -140,7 +140,7 @@ org.digijava.module.aim.dbentity.AmpLineMinistryObservationMeasure org.digijava.module.aim.dbentity.AmpLineMinistryObservationActor org.digijava.module.aim.dbentity.AmpComponentFunding - org.digijava.module.aim.dbentity.AmpRegionalFunding + org.digijava.module.aim.dbentity.AmpRegionalFunding org.digijava.module.aim.dbentity.AmpReportsOptions org.digijava.module.aim.dbentity.AmpIndicatorRiskRatings org.digijava.module.aim.dbentity.AmpAhsurvey @@ -152,14 +152,14 @@ org.digijava.module.aim.dbentity.AmpGPISurveyIndicator org.digijava.module.aim.dbentity.AmpGPISurveyQuestion org.digijava.module.aim.dbentity.AmpGPISurveyResponse - org.digijava.module.aim.dbentity.AmpGPISurveyQuestionType + org.digijava.module.aim.dbentity.AmpGPISurveyQuestionType org.digijava.module.aim.dbentity.AmpGPINiAidOnBudget org.digijava.module.aim.dbentity.AmpGPINiIndicator org.digijava.module.aim.dbentity.AmpGPINiQuestion org.digijava.module.aim.dbentity.AmpGPINiQuestionOption org.digijava.module.aim.dbentity.AmpGPINiSurvey org.digijava.module.aim.dbentity.AmpGPINiSurveyResponse - org.digijava.module.aim.dbentity.AmpGPINiSurveyResponseDocument + org.digijava.module.aim.dbentity.AmpGPINiSurveyResponseDocument org.digijava.module.aim.dbentity.AmpGPINiDonorNotes org.digijava.module.aim.dbentity.AmpFeature org.digijava.module.aim.dbentity.AmpSiteFlag @@ -195,60 +195,60 @@ org.digijava.module.aim.dbentity.AmpChapter org.digijava.module.aim.dbentity.AmpImputation org.digijava.module.aim.dbentity.AmpColorThreshold - - org.digijava.module.aim.dbentity.AmpActivityGroup + + org.digijava.module.aim.dbentity.AmpActivityGroup org.digijava.module.aim.dbentity.AmpActivityVersion org.digijava.module.aim.dbentity.AmpAnnualProjectBudget org.digijava.module.aim.dbentity.AmpFundingAmount - + org.digijava.module.fundingpledges.dbentity.FundingPledges org.digijava.module.fundingpledges.dbentity.FundingPledgesLocation org.digijava.module.fundingpledges.dbentity.FundingPledgesProgram org.digijava.module.fundingpledges.dbentity.FundingPledgesSector - org.digijava.module.fundingpledges.dbentity.FundingPledgesDocument + org.digijava.module.fundingpledges.dbentity.FundingPledgesDocument org.digijava.module.fundingpledges.dbentity.FundingPledgesDetails - + org.digijava.module.aim.dbentity.AmpOrgLocation org.digijava.module.aim.dbentity.AmpOrgStaffInformation org.digijava.module.aim.dbentity.AmpOrganizationBudgetInformation org.digijava.module.aim.dbentity.AmpOrgRecipient - + org.digijava.module.budget.dbentity.AmpDepartments org.digijava.module.budget.dbentity.AmpBudgetSector org.digijava.module.aim.dbentity.AmpContact org.digijava.module.aim.dbentity.AmpActivityContact org.digijava.module.aim.dbentity.AmpContactProperty org.digijava.module.aim.dbentity.AmpOrganisationContact - + org.digijava.module.aim.dbentity.AmpStructureType org.digijava.module.aim.dbentity.AmpStructure org.digijava.module.aim.dbentity.AmpStructureImg - org.digijava.module.aim.dbentity.AmpStructureCoordinate + org.digijava.module.aim.dbentity.AmpStructureCoordinate org.digijava.module.aim.dbentity.OnepagerSection org.digijava.module.aim.dbentity.AmpContentTranslation - + org.digijava.module.aim.dbentity.GPISetup org.digijava.module.aim.dbentity.GPIDefaultFilters org.digijava.module.aim.dbentity.AmpAidEffectivenessIndicator org.digijava.module.aim.dbentity.AmpAidEffectivenessIndicatorOption - + org.digijava.module.aim.dbentity.AmpInterchangeableResult - + org.digijava.module.aim.dbentity.AmpMenuEntry - + org.digijava.module.aim.dbentity.AmpVisibilityRule - + org.digijava.module.aim.dbentity.AmpPerformanceRule org.digijava.module.aim.dbentity.AmpPerformanceRuleAttribute - + org.digijava.module.aim.dbentity.AmpFileType @@ -270,6 +270,13 @@ org.digijava.module.aim.dbentity.AmpIndirectTheme org.digijava.module.aim.dbentity.AmpActivityIndirectProgram org.digijava.module.aim.dbentity.AmpThemeMapping + + + org.digijava.module.aim.action.dataimporter.dbentity.ImportedFilesRecord + org.digijava.module.aim.action.dataimporter.dbentity.ImportedProject + org.digijava.module.aim.action.dataimporter.dbentity.DataImporterConfig + org.digijava.module.aim.action.dataimporter.dbentity.DataImporterConfigValues + org.digijava.module.aim.action.dataimporter.dbentity.ImportedProjectCurrency diff --git a/amp/repository/aim/struts-config.xml b/amp/repository/aim/struts-config.xml index fe3b300533f..801657f82a9 100644 --- a/amp/repository/aim/struts-config.xml +++ b/amp/repository/aim/struts-config.xml @@ -1355,7 +1355,7 @@ - + diff --git a/amp/repository/aim/view/allVisibilityTags.jsp b/amp/repository/aim/view/allVisibilityTags.jsp index 7bf0ec33a5d..af169a274ac 100644 --- a/amp/repository/aim/view/allVisibilityTags.jsp +++ b/amp/repository/aim/view/allVisibilityTags.jsp @@ -12,29 +12,29 @@ <%@ taglib uri="/taglib/moduleVisibility" prefix="module" %> <%@ taglib uri="/taglib/jstl-functions" prefix="fn" %> - - + + - - - - - - - - - + + + + + + + + + - - + + @@ -58,32 +58,32 @@ - - + + - - - - - + + + + + - - - - + + + + - - - - - + + + + + - - - + + + @@ -93,8 +93,8 @@ - - + + @@ -120,7 +120,7 @@ - + @@ -129,7 +129,7 @@ - + @@ -144,131 +144,132 @@ - + - - - - - - - + + + + + + + - - + + - - - - + + + + - - - - + + + + - - + + + - - - - - - - - - + + + + + + + + + - - + + - + - - - - - - - - + + + + + + + + - - + + - - + + - - - - - + + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - + - - - + + + - - - - - + + + + + - - - - - - - + + + + + + + - + - + @@ -293,7 +294,7 @@ - + @@ -307,9 +308,9 @@ - - - + + + @@ -333,52 +334,52 @@ - - - + + + - + - - - - - + + + + + - - + + - - - - - - - - - + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + @@ -386,7 +387,7 @@ - + @@ -394,102 +395,102 @@ - - - - - - - + + + + + + + - - + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + - + - + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - + @@ -502,7 +503,7 @@ -<%-- +<%-- deleted 6 fields, do not reinsert --%> @@ -534,146 +535,146 @@ deleted 6 fields, do not reinsert - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - + - + - - - - - - + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - - - - + + + + + + + - - - + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - + + + - - - - + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - + - - + + - - + + @@ -681,7 +682,7 @@ deleted 6 fields, do not reinsert - + @@ -696,51 +697,51 @@ deleted 6 fields, do not reinsert - - - - - - - - - - - + + + + + + + + + + + - - - + + + - - - - - + + + + + - - - - - - - + + + + + + + - - - - - - - - + + + + + + + + @@ -751,91 +752,91 @@ deleted 6 fields, do not reinsert - - - - - - - + + + + + + + - - - - - - - - + + + + + + + + - - - - + + + + - + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + - + - - + + @@ -844,31 +845,31 @@ deleted 6 fields, do not reinsert - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - + + + + - - - - - - + + + + + + @@ -890,30 +891,30 @@ deleted 6 fields, do not reinsert - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + @@ -921,46 +922,46 @@ deleted 6 fields, do not reinsert - - - - - - + + + + + + - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + @@ -982,8 +983,8 @@ deleted 6 fields, do not reinsert - - + + @@ -993,7 +994,7 @@ deleted 6 fields, do not reinsert - + @@ -1006,11 +1007,11 @@ deleted 6 fields, do not reinsert - - + + - - + + @@ -1116,10 +1117,10 @@ deleted 6 fields, do not reinsert - - - - + + + + diff --git a/amp/repository/aim/view/dataImporter.jsp b/amp/repository/aim/view/dataImporter.jsp index 60a4d5d14b7..824ab0a3be5 100644 --- a/amp/repository/aim/view/dataImporter.jsp +++ b/amp/repository/aim/view/dataImporter.jsp @@ -28,15 +28,7 @@ } $(document).ready(function() { - // $('.fields-table tbody').on('click', '.remove-field', function() { - // console.log("Removing..."); - // var $row = $(this).closest('tr'); - // var selectedField = $row.find('.selected-field').text(); - // var columnName = $row.find('.column-name').text(); - // sendValuesToBackend(columnName,selectedField,"removeField"); - // - // }); - + $('#existing-config').val('0'); $('.remove-row').click(function() { var selectedRows = $('.fields-table tbody').find('.remove-checkbox:checked').closest('tr'); @@ -54,8 +46,94 @@ }); }); + $('.file_type').change(function() { + var fileType = $(this).val(); + console.log("File type selected: " + fileType==='excel'); + if (fileType === "csv") { + $('#select-file-label').html("Select csv file"); + $('#data-file').attr("accept", ".csv"); + $('#template-file').attr("accept", ".csv"); + $('#separator-div').hide(); + + } else if(fileType==="text") { + $('#select-file-label').html("Select text file"); + $('#data-file').attr("accept", ".txt"); + $('#template-file').attr("accept", ".txt"); + $('#separator-div').show(); + } + else if(fileType==="excel") { + $('#select-file-label').html("Select excel file"); + $('#data-file').attr("accept", ".xls,.xlsx"); + $('#template-file').attr("accept", ".xls,.xlsx"); + $('#separator-div').hide(); + } + else if(fileType==="json") { + $('#select-file-label').html("Select json file"); + $('#data-file').attr("accept", ".json"); + $('#template-file').attr("accept", ".json"); + $('#separator-div').hide(); + } }); + $('.existing-config').change(function() { + var configName = $(this).val(); + if (configName!=='none'){ + + var formData = new FormData(); + formData.append("configName", configName); + formData.append("action", "configByName"); + + fetch("${pageContext.request.contextPath}/aim/dataImporter.do", { + method: "POST", + body: formData + }) + .then(response =>{ + if (!response.ok) { + throw new Error("Network response was not ok"); + } + console.log("Response: ",response); + $("#templateUploadForm").hide(); + $('#existing-config').val('0'); + return response.json(); + }) + .then(updatedMap => { + console.log("Map :" ,updatedMap) + + // Update UI or perform any additional actions if needed + console.log("Selected pairs updated successfully."); + console.log("Updated map received:", updatedMap); + var tbody = document.getElementById("selected-pairs-table-body"); + + // Remove all rows from the table body + tbody.innerHTML = ""; + + for (var key in updatedMap) { + if (updatedMap.hasOwnProperty(key)) { + // Access each property using the key + var value = updatedMap[key]; + updateTable(key, value, tbody); + console.log('Key:', key, 'Value:', value); + } + } + document.getElementById("otherComponents").removeAttribute("hidden"); + $('#add-field').hide(); + $('.remove-row').hide(); + $('#selected-field').hide(); + $('#existing-config').val('1'); + + + }) + .catch(error => { + console.error("There was a problem with the fetch operation:", error); + }); + }else + { + $("#templateUploadForm").show(); + } + }); + }); + + function sendValuesToBackend(columnName, selectedField, action) { // Create a FormData object to send data in the request body var formData = new FormData(); @@ -133,10 +211,17 @@ } function uploadTemplateFile() { + $('#existing-config').val('0'); var formData = new FormData(); - var fileInput = document.getElementById('templateFile'); + var fileInput = document.getElementById('template-file'); + var fileType = $('#file-type').val(); + var dataSeparator = $('#data-separator').val(); + formData.append('templateFile', fileInput.files[0]); formData.append('action', "uploadTemplate"); + formData.append('uploadTemplate', "uploadTemplate"); + formData.append('fileType', fileType); + formData.append('dataSeparator', dataSeparator); var xhr = new XMLHttpRequest(); xhr.open('POST', '${pageContext.request.contextPath}/aim/dataImporter.do', true); @@ -146,6 +231,10 @@ document.getElementById('headers').innerHTML = xhr.getResponseHeader('selectTag'); alert("The template has been successfully uploaded."); document.getElementById("otherComponents").removeAttribute("hidden"); + $('#add-field').show(); + $('.remove-row').show(); + $('#selected-field').show(); + $(".fields-table").load(location.href + " .fields-table"); } else { console.error("Unable to extract headers.Please check the file format and try again"); alert("Unable to extract headers.Please check the file format and try again"); @@ -160,14 +249,24 @@ function uploadDataFile() { var formData = new FormData(); - var fileInput = document.getElementById('dataFile'); + var fileType = $('#file-type').val(); + var internal = $('#internal').prop('checked'); + console.log("Internal", internal); + var dataSeparator = $('#data-separator').val(); + var existingConfig = $('#existing-config').val(); + console.log("Existing configuration: " + existingConfig); + var fileInput = document.getElementById('data-file'); // Check if a file is selected if (!fileInput.files.length) { alert("Please select a file to upload."); return; } formData.append('dataFile', fileInput.files[0]); + formData.append('internal', internal); formData.append('action',"uploadDataFile"); + formData.append('fileType', fileType); + formData.append('dataSeparator', dataSeparator); + formData.append('existingConfig', existingConfig); var xhr = new XMLHttpRequest(); xhr.open('POST', '${pageContext.request.contextPath}/aim/dataImporter.do', true); @@ -208,25 +307,65 @@

Data Importer

+ + + +

Upload File

Data file configuration

-
+ +
+ + + + + +
+ + + + + + - + +
+

-