From 17a80d36bcbd36902a02f515da1084b9e79ed129 Mon Sep 17 00:00:00 2001 From: avikganguly01 Date: Sun, 16 Jul 2017 22:54:14 +0530 Subject: [PATCH] MifosX-1127 --- .../populator/AbstractWorkbookPopulator.java | 100 +++++++++++ .../populator/OfficeSheetPopulator.java | 72 ++++++++ .../populator/PersonnelSheetPopulator.java | 127 ++++++++++++++ .../populator/WorkbookPopulator.java | 27 +++ .../client/ClientWorkbookPopulator.java | 160 ++++++++++++++++++ .../BulkImportWorkbookPopulatorService.java | 27 +++ ...ulkImportWorkbookPopulatorServiceImpl.java | 127 ++++++++++++++ .../BulkImportWritePlatformService.java | 27 +++ ...WritePlatformServiceJpaRepositoryImpl.java | 35 ++++ .../organisation/office/data/OfficeData.java | 8 + 10 files changed, 710 insertions(+) create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/AbstractWorkbookPopulator.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/OfficeSheetPopulator.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/PersonnelSheetPopulator.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/WorkbookPopulator.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/client/ClientWorkbookPopulator.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorService.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorServiceImpl.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformService.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformServiceJpaRepositoryImpl.java diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/AbstractWorkbookPopulator.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/AbstractWorkbookPopulator.java new file mode 100644 index 00000000000..fa0b6bf594f --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/AbstractWorkbookPopulator.java @@ -0,0 +1,100 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.populator; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import org.apache.fineract.organisation.office.data.OfficeData; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +public abstract class AbstractWorkbookPopulator implements WorkbookPopulator { + + protected void writeInt(int colIndex, Row row, int value) { + row.createCell(colIndex).setCellValue(value); + } + + protected void writeLong(int colIndex, Row row, long value) { + row.createCell(colIndex).setCellValue(value); + } + + protected void writeString(int colIndex, Row row, String value) { + row.createCell(colIndex).setCellValue(value); + } + + protected void writeDouble(int colIndex, Row row, double value) { + row.createCell(colIndex).setCellValue(value); + } + + protected void writeFormula(int colIndex, Row row, String formula) { + row.createCell(colIndex).setCellType(Cell.CELL_TYPE_FORMULA); + row.createCell(colIndex).setCellFormula(formula); + } + + protected CellStyle getDateCellStyle(Workbook workbook) { + CellStyle dateCellStyle = workbook.createCellStyle(); + short df = workbook.createDataFormat().getFormat("dd/mm/yy"); + dateCellStyle.setDataFormat(df); + return dateCellStyle; + } + + protected void writeDate(int colIndex, Row row, String value, CellStyle dateCellStyle) { + try { + // To make validation between functions inclusive. + Date date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(value); + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + Date dateWithoutTime = cal.getTime(); + row.createCell(colIndex).setCellValue(dateWithoutTime); + row.getCell(colIndex).setCellStyle(dateCellStyle); + } catch (ParseException pe) { + throw new IllegalArgumentException("ParseException"); + } + } + + protected void setOfficeDateLookupTable(Sheet sheet, List offices, int officeNameCol, + int activationDateCol) { + Workbook workbook = sheet.getWorkbook(); + CellStyle dateCellStyle = workbook.createCellStyle(); + short df = workbook.createDataFormat().getFormat("dd/mm/yy"); + dateCellStyle.setDataFormat(df); + int rowIndex = 0; + for (OfficeData office : offices) { + Row row = sheet.createRow(++rowIndex); + writeString(officeNameCol, row, office.name().trim().replaceAll("[ )(]", "_")); + writeDate(activationDateCol, row, + "" + office.getOpeningDate().getDayOfMonth() + "/" + + office.getOpeningDate().getMonthOfYear() + "/" + office.getOpeningDate().getYear(), + dateCellStyle); + } + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/OfficeSheetPopulator.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/OfficeSheetPopulator.java new file mode 100644 index 00000000000..fa4822603b0 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/OfficeSheetPopulator.java @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.populator; + +import java.util.List; + +import org.apache.fineract.organisation.office.data.OfficeData; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +public class OfficeSheetPopulator extends AbstractWorkbookPopulator { + + private List offices; + + private static final int ID_COL = 0; + private static final int OFFICE_NAME_COL = 1; + + public OfficeSheetPopulator(final List offices) { + this.offices = offices; + } + + + @Override + public void populate(final Workbook workbook) { + int rowIndex = 1; + Sheet officeSheet = workbook.createSheet("Offices"); + setLayout(officeSheet); + + populateOffices(officeSheet, rowIndex); + officeSheet.protectSheet(""); + } + + private void populateOffices(Sheet officeSheet, int rowIndex) { + for (OfficeData office : offices) { + Row row = officeSheet.createRow(rowIndex); + writeLong(ID_COL, row, office.getId()); + writeString(OFFICE_NAME_COL, row, office.name().trim().replaceAll("[ )(]", "_")); + rowIndex++; + } + } + + private void setLayout(Sheet worksheet) { + worksheet.setColumnWidth(ID_COL, 2000); + worksheet.setColumnWidth(OFFICE_NAME_COL, 7000); + Row rowHeader = worksheet.createRow(0); + rowHeader.setHeight((short) 500); + writeString(ID_COL, rowHeader, "ID"); + writeString(OFFICE_NAME_COL, rowHeader, "Name"); + } + + public List getOffices() { + return offices; + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/PersonnelSheetPopulator.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/PersonnelSheetPopulator.java new file mode 100644 index 00000000000..a5f82580a5f --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/PersonnelSheetPopulator.java @@ -0,0 +1,127 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.populator; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.fineract.organisation.office.data.OfficeData; +import org.apache.fineract.organisation.staff.data.StaffData; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +public class PersonnelSheetPopulator extends AbstractWorkbookPopulator { + + private List personnel; + private List offices; + + // Maintaining the one to many relationship + private Map> officeToPersonnel; + + /* + * Guava Multimap would make this more readable. The value Integer[] contains the beginIndex and + * endIndex for the staff list of each office. Required for applying names in excel. + */ + private Map officeNameToBeginEndIndexesOfStaff; + + private static final int OFFICE_NAME_COL = 0; + private static final int STAFF_NAME_COL = 1; + private static final int STAFF_ID_COL = 2; + + public PersonnelSheetPopulator(List personnel, List offices) { + this.personnel = personnel; + this.offices = offices; + } + + + @Override + public void populate(Workbook workbook) { + Sheet staffSheet = workbook.createSheet("Staff"); + setLayout(staffSheet); + + /* + * This piece of code could have been avoided by making multiple trips to the database for the + * staff of each office but this is more performance efficient + */ + setOfficeToPersonnelMap(); + + populateStaffByOfficeName(staffSheet); + staffSheet.protectSheet(""); + } + + + private void populateStaffByOfficeName(Sheet staffSheet) { + int rowIndex = 1, startIndex = 1, officeIndex = 0; + officeNameToBeginEndIndexesOfStaff = new HashMap(); + Row row = staffSheet.createRow(rowIndex); + for (OfficeData office : offices) { + startIndex = rowIndex + 1; + writeString(OFFICE_NAME_COL, row, office.name().trim().replaceAll("[ )(]", "_")); + + List staffList = + officeToPersonnel.get(office.name().trim().replaceAll("[ )(]", "_")); + + if (!CollectionUtils.isEmpty(staffList)) { + for (StaffData staff : staffList) { + writeString(STAFF_NAME_COL, row, staff.getDisplayName()); + writeLong(STAFF_ID_COL, row, staff.getId()); + row = staffSheet.createRow(++rowIndex); + } + officeNameToBeginEndIndexesOfStaff.put(officeIndex++, new Integer[] {startIndex, rowIndex}); + } else + officeIndex++; + } + } + + private void setOfficeToPersonnelMap() { + officeToPersonnel = new HashMap>(); + for (StaffData person : personnel) { + add(person.getOfficeName().trim().replaceAll("[ )(]", "_"), person); + } + } + + // Guava Multi-map can reduce this. + private void add(String key, StaffData value) { + List values = officeToPersonnel.get(key); + if (values == null) { + values = new ArrayList(); + } + values.add(value); + officeToPersonnel.put(key, values); + } + + private void setLayout(Sheet worksheet) { + for (Integer i = 0; i < 3; i++) + worksheet.setColumnWidth(i, 6000); + Row rowHeader = worksheet.createRow(0); + rowHeader.setHeight((short) 500); + writeString(OFFICE_NAME_COL, rowHeader, "Office Name"); + writeString(STAFF_NAME_COL, rowHeader, "Staff List"); + writeString(STAFF_ID_COL, rowHeader, "Staff ID"); + } + + public Map getOfficeNameToBeginEndIndexesOfStaff() { + return officeNameToBeginEndIndexesOfStaff; + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/WorkbookPopulator.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/WorkbookPopulator.java new file mode 100644 index 00000000000..995743d41fe --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/WorkbookPopulator.java @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.populator; + +import org.apache.poi.ss.usermodel.Workbook; + +public interface WorkbookPopulator { + + void populate(Workbook workbook); + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/client/ClientWorkbookPopulator.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/client/ClientWorkbookPopulator.java new file mode 100644 index 00000000000..51fffb2907d --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/populator/client/ClientWorkbookPopulator.java @@ -0,0 +1,160 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.populator.client; + +import java.util.List; + +import org.apache.fineract.infrastructure.bulkimport.populator.AbstractWorkbookPopulator; +import org.apache.fineract.infrastructure.bulkimport.populator.OfficeSheetPopulator; +import org.apache.fineract.infrastructure.bulkimport.populator.PersonnelSheetPopulator; +import org.apache.fineract.organisation.office.data.OfficeData; +import org.apache.poi.hssf.usermodel.HSSFDataValidationHelper; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.ss.SpreadsheetVersion; +import org.apache.poi.ss.usermodel.DataValidation; +import org.apache.poi.ss.usermodel.DataValidationConstraint; +import org.apache.poi.ss.usermodel.DataValidationHelper; +import org.apache.poi.ss.usermodel.Name; +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.ss.util.CellRangeAddressList; + +public class ClientWorkbookPopulator extends AbstractWorkbookPopulator { + + private static final int FIRST_NAME_COL = 0; + private static final int LAST_NAME_COL = 1; + private static final int MIDDLE_NAME_COL = 2; + private static final int OFFICE_NAME_COL = 3; + private static final int STAFF_NAME_COL = 4; + private static final int EXTERNAL_ID_COL = 5; + private static final int ACTIVATION_DATE_COL = 6; + private static final int ACTIVE_COL = 7; + private static final int WARNING_COL = 9; + private static final int RELATIONAL_OFFICE_NAME_COL = 16; + private static final int RELATIONAL_OFFICE_OPENING_DATE_COL = 17; + + private OfficeSheetPopulator officeSheetPopulator; + private PersonnelSheetPopulator personnelSheetPopulator; + + public ClientWorkbookPopulator(OfficeSheetPopulator officeSheetPopulator, + PersonnelSheetPopulator personnelSheetPopulator) { + this.officeSheetPopulator = officeSheetPopulator; + this.personnelSheetPopulator = personnelSheetPopulator; + } + + + @Override + public void populate(Workbook workbook) { + Sheet clientSheet = workbook.createSheet("Clients"); + personnelSheetPopulator.populate(workbook); + officeSheetPopulator.populate(workbook); + setLayout(clientSheet); + setOfficeDateLookupTable(clientSheet, officeSheetPopulator.getOffices(), + RELATIONAL_OFFICE_NAME_COL, RELATIONAL_OFFICE_OPENING_DATE_COL); + setRules(clientSheet); + } + + private void setLayout(Sheet worksheet) { + Row rowHeader = worksheet.createRow(0); + rowHeader.setHeight((short) 500); + worksheet.setColumnWidth(FIRST_NAME_COL, 6000); + worksheet.setColumnWidth(LAST_NAME_COL, 6000); + worksheet.setColumnWidth(MIDDLE_NAME_COL, 6000); + writeString(FIRST_NAME_COL, rowHeader, "First Name*"); + writeString(LAST_NAME_COL, rowHeader, "Last Name*"); + writeString(MIDDLE_NAME_COL, rowHeader, "Middle Name"); + worksheet.setColumnWidth(OFFICE_NAME_COL, 5000); + worksheet.setColumnWidth(STAFF_NAME_COL, 5000); + worksheet.setColumnWidth(EXTERNAL_ID_COL, 3500); + worksheet.setColumnWidth(ACTIVATION_DATE_COL, 4000); + worksheet.setColumnWidth(ACTIVE_COL, 2000); + worksheet.setColumnWidth(RELATIONAL_OFFICE_NAME_COL, 6000); + worksheet.setColumnWidth(RELATIONAL_OFFICE_OPENING_DATE_COL, 4000); + writeString(OFFICE_NAME_COL, rowHeader, "Office Name*"); + writeString(STAFF_NAME_COL, rowHeader, "Staff Name*"); + writeString(EXTERNAL_ID_COL, rowHeader, "External ID"); + writeString(ACTIVATION_DATE_COL, rowHeader, "Activation Date*"); + writeString(ACTIVE_COL, rowHeader, "Active*"); + writeString(WARNING_COL, rowHeader, "All * marked fields are compulsory."); + writeString(RELATIONAL_OFFICE_NAME_COL, rowHeader, "Office Name"); + writeString(RELATIONAL_OFFICE_OPENING_DATE_COL, rowHeader, "Opening Date"); + + } + + private void setRules(Sheet worksheet) { + CellRangeAddressList officeNameRange = new CellRangeAddressList(1, + SpreadsheetVersion.EXCEL97.getLastRowIndex(), OFFICE_NAME_COL, OFFICE_NAME_COL); + CellRangeAddressList staffNameRange = new CellRangeAddressList(1, + SpreadsheetVersion.EXCEL97.getLastRowIndex(), STAFF_NAME_COL, STAFF_NAME_COL); + CellRangeAddressList dateRange = new CellRangeAddressList(1, + SpreadsheetVersion.EXCEL97.getLastRowIndex(), ACTIVATION_DATE_COL, ACTIVATION_DATE_COL); + CellRangeAddressList activeRange = new CellRangeAddressList(1, + SpreadsheetVersion.EXCEL97.getLastRowIndex(), ACTIVE_COL, ACTIVE_COL); + + DataValidationHelper validationHelper = new HSSFDataValidationHelper((HSSFSheet) worksheet); + + List offices = officeSheetPopulator.getOffices(); + setNames(worksheet, offices); + + DataValidationConstraint officeNameConstraint = + validationHelper.createFormulaListConstraint("Office"); + DataValidationConstraint staffNameConstraint = + validationHelper.createFormulaListConstraint("INDIRECT(CONCATENATE(\"Staff_\",$D1))"); + DataValidationConstraint activationDateConstraint = + validationHelper.createDateConstraint(DataValidationConstraint.OperatorType.BETWEEN, + "=VLOOKUP($D1,$Q$2:$R" + (offices.size() + 1) + ",2,FALSE)", "=TODAY()", "dd/mm/yy"); + DataValidationConstraint activeConstraint = + validationHelper.createExplicitListConstraint(new String[] {"True", "False"}); + + + DataValidation officeValidation = + validationHelper.createValidation(officeNameConstraint, officeNameRange); + DataValidation staffValidation = + validationHelper.createValidation(staffNameConstraint, staffNameRange); + DataValidation activationDateValidation = + validationHelper.createValidation(activationDateConstraint, dateRange); + DataValidation activeValidation = + validationHelper.createValidation(activeConstraint, activeRange); + + worksheet.addValidationData(activeValidation); + worksheet.addValidationData(officeValidation); + worksheet.addValidationData(staffValidation); + worksheet.addValidationData(activationDateValidation); + } + + private void setNames(Sheet worksheet, List offices) { + Workbook clientWorkbook = worksheet.getWorkbook(); + Name officeGroup = clientWorkbook.createName(); + officeGroup.setNameName("Office"); + officeGroup.setRefersToFormula("Offices!$B$2:$B$" + (offices.size() + 1)); + + for (Integer i = 0; i < offices.size(); i++) { + Integer[] officeNameToBeginEndIndexesOfStaff = + personnelSheetPopulator.getOfficeNameToBeginEndIndexesOfStaff().get(i); + if (officeNameToBeginEndIndexesOfStaff != null) { + Name name = clientWorkbook.createName(); + name.setNameName("Staff_" + offices.get(i).name().trim().replaceAll("[ )(]", "_")); + name.setRefersToFormula("Staff!$B$" + officeNameToBeginEndIndexesOfStaff[0] + ":$B$" + + officeNameToBeginEndIndexesOfStaff[1]); + } + } + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorService.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorService.java new file mode 100644 index 00000000000..b180f8abc73 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorService.java @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.service; + +import javax.ws.rs.core.Response; + +public interface BulkImportWorkbookPopulatorService { + + public Response getTemplate(final String entityType, final Long officeId, final Long staffId); + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorServiceImpl.java new file mode 100644 index 00000000000..51df11bb583 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWorkbookPopulatorServiceImpl.java @@ -0,0 +1,127 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.service; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; + +import org.apache.fineract.infrastructure.bulkimport.populator.OfficeSheetPopulator; +import org.apache.fineract.infrastructure.bulkimport.populator.PersonnelSheetPopulator; +import org.apache.fineract.infrastructure.bulkimport.populator.WorkbookPopulator; +import org.apache.fineract.infrastructure.bulkimport.populator.client.ClientWorkbookPopulator; +import org.apache.fineract.infrastructure.core.exception.GeneralPlatformDomainRuleException; +import org.apache.fineract.infrastructure.core.service.DateUtils; +import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; +import org.apache.fineract.organisation.office.data.OfficeData; +import org.apache.fineract.organisation.office.service.OfficeReadPlatformService; +import org.apache.fineract.organisation.staff.data.StaffData; +import org.apache.fineract.organisation.staff.service.StaffReadPlatformService; +import org.apache.fineract.portfolio.client.api.ClientApiConstants; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Workbook; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class BulkImportWorkbookPopulatorServiceImpl implements BulkImportWorkbookPopulatorService { + + private final PlatformSecurityContext context; + private final OfficeReadPlatformService officeReadPlatformService; + private final StaffReadPlatformService staffReadPlatformService; + + @Autowired + public BulkImportWorkbookPopulatorServiceImpl(final PlatformSecurityContext context, + final OfficeReadPlatformService officeReadPlatformService, + final StaffReadPlatformService staffReadPlatformService) { + this.officeReadPlatformService = officeReadPlatformService; + this.staffReadPlatformService = staffReadPlatformService; + this.context = context; + } + + @Override + public Response getTemplate(final String entityType, final Long officeId, final Long staffId) { + + WorkbookPopulator populator = null; + final Workbook workbook = new HSSFWorkbook(); + if (entityType.trim().equalsIgnoreCase(ClientApiConstants.CLIENT_RESOURCE_NAME)) { + populator = populateClientWorkbook(officeId, staffId); + } else + throw new GeneralPlatformDomainRuleException("error.msg.unable.to.find.resource", + "Unable to find requested resource"); + populator.populate(workbook); + return buildResponse(workbook, entityType); + } + + private WorkbookPopulator populateClientWorkbook(final Long officeId, final Long staffId) { + this.context.authenticatedUser().validateHasReadPermission("OFFICE"); + this.context.authenticatedUser().validateHasReadPermission("STAFF"); + List offices = fetchOffices(officeId); + List staff = fetchStaff(staffId); + + return new ClientWorkbookPopulator(new OfficeSheetPopulator(offices), + new PersonnelSheetPopulator(staff, offices)); + } + + private Response buildResponse(final Workbook workbook, final String entity) { + String filename = entity + DateUtils.getLocalDateOfTenant().toString() + ".xls"; + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + workbook.write(baos); + } catch (IOException e) { + e.printStackTrace(); + } + + final ResponseBuilder response = Response.ok(baos.toByteArray()); + response.header("Content-Disposition", "attachment; filename=\"" + filename + "\""); + response.header("Content-Type", "application/vnd.ms-excel"); + return response.build(); + } + + @SuppressWarnings("unchecked") + private List fetchOffices(final Long officeId) { + List offices = null; + if (officeId == null) { + Boolean includeAllOffices = Boolean.TRUE; + offices = (List) this.officeReadPlatformService.retrieveAllOffices(includeAllOffices, null); + } else { + offices = new ArrayList<>(); + offices.add(this.officeReadPlatformService.retrieveOffice(officeId)); + } + return offices; + } + + @SuppressWarnings("unchecked") + private List fetchStaff(final Long staffId) { + List staff = null; + if (staffId == null) + staff = + (List) this.staffReadPlatformService.retrieveAllStaff(null, null, Boolean.FALSE, null); + else { + staff = new ArrayList<>(); + staff.add(this.staffReadPlatformService.retrieveStaff(staffId)); + } + return staff; + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformService.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformService.java new file mode 100644 index 00000000000..b0deb44fdd6 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformService.java @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.service; + +import java.io.InputStream; + +public interface BulkImportWritePlatformService { + + public void importTemplateData(final String entityType, final InputStream uploadedInputStream); + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformServiceJpaRepositoryImpl.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformServiceJpaRepositoryImpl.java new file mode 100644 index 00000000000..4c9f1df1237 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/bulkimport/service/BulkImportWritePlatformServiceJpaRepositoryImpl.java @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.infrastructure.bulkimport.service; + +import java.io.InputStream; + +import org.springframework.stereotype.Service; + +@Service +public class BulkImportWritePlatformServiceJpaRepositoryImpl + implements BulkImportWritePlatformService { + + @Override + public void importTemplateData(String entityType, InputStream uploadedInputStream) { + // TODO Auto-generated method stub + + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/organisation/office/data/OfficeData.java b/fineract-provider/src/main/java/org/apache/fineract/organisation/office/data/OfficeData.java index 3ea04ee93fa..0fea1edea6b 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/organisation/office/data/OfficeData.java +++ b/fineract-provider/src/main/java/org/apache/fineract/organisation/office/data/OfficeData.java @@ -77,4 +77,12 @@ public String name() { public String getHierarchy() { return this.hierarchy; } + + public Long getId() { + return id; + } + + public LocalDate getOpeningDate() { + return openingDate; + } } \ No newline at end of file