Skip to content

Commit

Permalink
WIP org upload #3357
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Nov 12, 2024
1 parent e8741b5 commit d6a87ac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 144 deletions.
154 changes: 18 additions & 136 deletions grails-app/controllers/au/org/ala/merit/AdminController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import grails.util.Environment
import grails.util.GrailsNameUtils
import grails.web.http.HttpHeaders
import groovy.util.logging.Slf4j
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.ss.util.CellReference
Expand Down Expand Up @@ -482,136 +484,6 @@ class AdminController {
}
}

def allScores() {
def scores = []
def activityModel = metadataService.activitiesModel()
activityModel.outputs.each { output ->
def outputScores = output.scores.findAll{it.isOutputTarget}
outputScores.each {
scores << [output:output.name] + it
}
}

render scores as JSON


}


def createMissingOrganisations() {

def results = [errors:[], messages:[]]
if (request instanceof MultipartHttpServletRequest) {
def file = request.getFile('orgData')
if (file) {
CSVReader reader = new CSVReader(new InputStreamReader(file.inputStream, 'UTF-8'))
String[] line = reader.readNext()
line = reader.readNext() // Discard header line
while (line) {
def currentOrgName = line[0]
def correctOrgName = line[3]

def orgResults = createOrg(currentOrgName, correctOrgName)
results.errors += orgResults.errors
results.messages += orgResults.messages

line = reader.readNext()
}
}

}
def jsonResults = results as JSON
new File('/tmp/organisation_creation_results.json').withPrintWriter { pw ->
pw.print jsonResults.toString(true)
}

render results as JSON
}
def createOrg(String existingOrgName, String correctOrgName) {

def errors = []
def messages = []
def existingOrganisations = metadataService.organisationList()

def orgName = correctOrgName ?: existingOrgName

def organisationId
def organisation = existingOrganisations.list.find{it.name == orgName}
if (!organisation) {
def resp = organisationService.update('', [name:orgName, sourceSystem:'merit'])

organisationId = resp?.resp?.organisationId
if (!organisationId) {
errors << "Error creating organisation ${orgName} - ${resp?.error}"
return [errors:errors]
}
else {
messages << "Created organisation with name: ${orgName}"
}

}
else {
organisationId = organisation.organisationId
messages << "Organisation with name: ${orgName} already exists"
}


def projectsResp = projectService.search([organisationName:existingOrgName])
if (projectsResp?.resp.projects) {
def projects = projectsResp.resp.projects
messages << "Found ${projects.size()} projects with name ${existingOrgName}"
projects.each { project ->
if (project.organisationId != organisationId || project.organisationName != orgName) {
def resp = projectService.update(project.projectId, [organisationName:orgName, organisationId:organisationId])
if (!resp || resp.error) {
errors << "Error updating project ${project.projectId}"
}
else {
messages << "Updated project ${project.projectId} organisation to ${orgName}"
}
}
else {
messages << "Project ${project.projectId} already had correct organisation details"
}

}
}
else {
if (projectsResp?.resp?.projects?.size() == 0) {
messages << "Organisation ${existingOrgName} has no projects"
}
else {
errors << "Error retreiving projects for organisation ${existingOrgName} - ${projectsResp.error}"
}
}
return [errors:errors, messages:messages]

}

def createReports() {
def offset = 0
def max = 100

def projects = searchService.allProjects([max:max, offset:offset])

while (offset < projects.hits.total) {

offset+=max
projects = searchService.allProjects([max:max, offset:offset])

projects.hits?.hits?.each { hit ->
def project = hit._source
if (!project.timeline) {
projectService.generateProjectStageReports(project.projectId, new ReportGenerationOptions())
println "Generated reports for project ${project.projectId}"
}
}
println offset

}

}

def editSiteBlog() {
List<Map> blog = blogService.getSiteBlog()
[blog:blog]
Expand Down Expand Up @@ -720,22 +592,32 @@ class AdminController {

def organisationModifications() {
if (request.respondsTo('getFile')) {
def file = request.getFile('data')
def file = request.getFile('orgData')
Map results = [:]
if (file) {

def columnMap = [
'Project ID': 'projectId',
2: 'organisationId',
3: 'organisationName',
4: 'abn'
'Organisation ID': 'organisationId',
'Contract name': 'organisationContractName',
'ABN': 'abn',
'New contract name': 'newContractName'
]

def config = [
sheet : "Projects",
sheet : "Organisation Details",
startRow : 1,
columnMap: columnMap
columnMap: [:]
]
Workbook workbook = WorkbookFactory.create(file.inputStream)
Row headerRow = workbook.getSheet(config.sheet).getRow(0)
for (Cell c : headerRow) {
String headerValue = c.getStringCellValue()
if (columnMap.containsKey(headerValue)) {
String excelColumnHeader = CellReference.convertNumToColString(c.getColumnIndex())
config.columnMap[excelColumnHeader] = columnMap[headerValue]
}
}

List data = excelImportService.convertColumnMapConfigManyRows(workbook, config)
data.each { Map row ->
Expand Down
10 changes: 5 additions & 5 deletions grails-app/views/admin/tools.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@
$("#createOrgs").change(function() {
if ($("#createOrgs").val()) {
$("#btnCreateOrgs").removeAttr("disabled");
$("#btnUpdateProjectOrgs").removeAttr("disabled");
}
else {
$("#btnCreateOrgs").attr("disabled", "disabled");
$("#btnUpdateProjectOrgs").attr("disabled", "disabled");
}
}).trigger('change');
$('#btnCreateOrgs').click(function(e) {
$('#btnUpdateProjectOrgs').click(function(e) {
e.preventDefault();
$('form.createOrgs').submit();
});
Expand Down Expand Up @@ -262,11 +262,11 @@
</td>
</tr>
<tr>
<td><button disabled id="btnCreateOrgs" class="btn btn-sm btn-info" title="Bulk create organisations">Bulk create organisations</button>
<td><button disabled id="btnUpdateProjectOrgs" class="btn btn-sm btn-info" title="Bulk create organisations">Bulk create organisations</button>
</td>
<td>
Bulk creates organisations and updates projects.
<g:uploadForm class="createOrgs" action="createMissingOrganisations">
<g:uploadForm class="createOrgs" action="organisationModifications">
<div><input id="createOrgs" type="file" name="orgData"/></div>
</g:uploadForm>

Expand Down
11 changes: 8 additions & 3 deletions src/main/groovy/au/org/ala/merit/GmsMapper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,13 @@ class GmsMapper {
} else {
createOrganisation = true

String name = abnLookup.businessNames ? abnLookup.businessNames[0] : abnLookup.entityName
organisation = abnLookup + [name: name]
if (contractName) {
organisation = abnLookup + [name:contractName, contractNames: [contractName]]
}
else {
String name = abnLookup.businessNames ? abnLookup.businessNames[0] : abnLookup.entityName
organisation = abnLookup + [name: name]
}
messages << "An organisation will be created with ABN: ${abn} and name: ${name}"
}
} else {
Expand All @@ -306,7 +311,7 @@ class GmsMapper {

// Validate we can use the contract name
if (organisation && contractName) {
List names = [organisation.name] + organisation.businessNames
List names = [organisation.name] + organisation.contractNames
if (contractName && !contractName in names) {
error = "The organisation name in the contract ${contractName} doesn't match a known organisation name"
}
Expand Down

0 comments on commit d6a87ac

Please sign in to comment.