Commit fe6cbd05 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '168-upload-repository-metadata-for-institutes' into 'master'

Resolve "Upload repository metadata for institutes"

Closes #168

See merge request genesys-pgr/genesys-server!107
parents 60065f11 f56ec599
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed 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.genesys2.server.service.impl;
import org.apache.commons.lang.ArrayUtils;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author Andrey Lugovskoy.
*/
@Component
public class FilesMetadataUpdate extends ImportFromCSV<RepositoryFile> {
@Autowired
private RepositoryService repositoryService;
private final String[] CSV_HEADERS = new String[] { "uuid", "version", "contentType", "path", "extension", "originalFilename", "title", "subject", "description", "creator",
"created", "rightsHolder", "accessRights", "license", "bibliographicCitation" };
@Override
public List<RepositoryFile> updateFromCsv(InputStream str, char separator, char quoteChar, char escapeChar) {
List<RepositoryFile> files = uploadData(str, separator, quoteChar, escapeChar);
List<RepositoryFile> updatedFiles = new ArrayList<>();
files.forEach(element -> updatedFiles.add(updateValueOfObject(element)));
return updatedFiles;
}
@Override
protected void verifyHeaders(String[] headers) throws IOException {
LOG.info("Got headers: {}", ArrayUtils.toString(headers));
if (CSV_HEADERS.length != headers.length) {
throw new IOException("CSV header count mismatch. Found: " + ArrayUtils.toString(headers));
}
for (int i=0; i<headers.length; i++) {
if (! CSV_HEADERS[i].equalsIgnoreCase(headers[i])) {
throw new IOException("Header mismatch at position " + i + ". Found " + headers[i] + " instead of " + CSV_HEADERS[i]);
}
}
}
@Override
@Transactional(readOnly = false)
protected RepositoryFile updateValueOfObject(RepositoryFile sourceFile) {
RepositoryFile updatedFile = null;
try {
updatedFile = repositoryService.updateMetadata(sourceFile.getUuid(), sourceFile);
} catch (NoSuchRepositoryFileException e) {
LOG.error("Can not find RepositoryFile with uuid:{}", sourceFile.getUuid());
}
return updatedFile;
}
@Override
protected RepositoryFile prepareObject(String[] recordLine) throws IOException {
LOG.debug("Importing: {}", ArrayUtils.toString(recordLine));
if (CSV_HEADERS.length != recordLine.length) {
throw new IOException("CSV header count mismatch. Found: " + ArrayUtils.toString(recordLine));
}
RepositoryFile repositoryFile = null;
try {
repositoryFile = repositoryService.getFile(UUID.fromString(recordLine[0]));
// TODO must test that file belongs to the institute
// repositoryFile.setContentType(recordLine[2]);
// repositoryFile.setPath(recordLine[3]);
// repositoryFile.setExtent(recordLine[4]);
repositoryFile.setOriginalFilename(recordLine[5]);
repositoryFile.setTitle(recordLine[6]);
repositoryFile.setSubject(recordLine[7]);
repositoryFile.setDescription(recordLine[8]);
repositoryFile.setCreator(recordLine[9]);
repositoryFile.setCreated(recordLine[10]);
repositoryFile.setRightsHolder(recordLine[11]);
repositoryFile.setAccessRights(recordLine[12]);
repositoryFile.setLicense(recordLine[13]);
repositoryFile.setBibliographicCitation(recordLine[14]);
} catch (NoSuchRepositoryFileException e) {
LOG.warn("Could not find file uuid={}", recordLine[0]);
}
return repositoryFile;
}
}
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed 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.genesys2.server.service.impl;
import com.opencsv.CSVReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* @author Andrey Lugovskoy.
*/
public abstract class ImportFromCSV<T> {
public static final Logger LOG = LoggerFactory.getLogger(ImportFromCSV.class);
protected List<T> uploadData(final InputStream str, char separator, char quoteChar, char escapeChar) {
List<T> list = new ArrayList<>();
try (CSVReader reader = new CSVReader(new BufferedReader(new InputStreamReader(str, "UTF-8")), separator, quoteChar, escapeChar, 0, false, false)) {
// header line
String[] recordLine = reader.readNext();
verifyHeaders(recordLine);
while ((recordLine = reader.readNext()) != null) {
list.add(prepareObject(recordLine));
}
} catch (IOException e) {
LOG.error("CSV header count mismatch.", e);
}
return list;
}
/**
* Tests if header line contains the expected headers
*
* @param headers
* @throws IOException when headers don't match
*/
protected abstract void verifyHeaders(String[] headers) throws IOException;
public abstract List<T> updateFromCsv(final InputStream str, char separator, char quoteChar, char escapeChar);
protected abstract T updateValueOfObject(T target);
protected abstract T prepareObject(String[] recordLine) throws IOException;
}
......@@ -54,6 +54,7 @@ import org.genesys2.server.service.OrganizationService;
import org.genesys2.server.service.StatisticsService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.impl.FilesMetadataInfo;
import org.genesys2.server.service.impl.FilesMetadataUpdate;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -128,6 +129,8 @@ public class WiewsController extends BaseController {
@Autowired(required = false)
private FilesMetadataInfo filesMetadataInfo;
@Autowired
private FilesMetadataUpdate filesMetadataUpdate;
@RequestMapping("/")
public String view(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
......@@ -677,6 +680,15 @@ public class WiewsController extends BaseController {
filesMetadataInfo.downloadMetadata(files, response, '\t', '"', '\\', "\n", "UTF-16LE");
}
@PostMapping(value = "/{wiewsCode}/files/upload/metadata")
public String uploadMetadata(@RequestParam MultipartFile file, @PathVariable("wiewsCode") String wiewsCode) throws IOException {
instituteService.getInstituteForEdit(wiewsCode);
filesMetadataUpdate.updateFromCsv(file.getInputStream(), '\t', '"', '\\');
return "redirect:/wiews/" + wiewsCode + "/files";
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
......
......@@ -900,3 +900,4 @@ article.template.false=Article is not a template.
article.template.true=This is a template.
auditLog.no-value=No value
file.upload.metadata=Upload metadata
......@@ -860,3 +860,4 @@ welcome.networks=الشبكات
geo.country=الدولة:
file.download-metadata=تنزيل البيانات الوصفية
article.is.template=هو القالب:
file.upload.metadata=تحميل البيانات الوصفية
......@@ -860,3 +860,4 @@ welcome.networks=Netzwerke
geo.country=Land:
file.download-metadata=Metadaten herunterladen
article.is.template=Ist Vorlage:
file.upload.metadata=Metadaten hochladen
......@@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
file.upload.metadata=Upload metadata
......@@ -860,3 +860,4 @@ welcome.networks=Redes
geo.country=País:
file.download-metadata=Descargar metadatos
article.is.template=Es plantilla:
file.upload.metadata=Cargar metadatos
......@@ -860,3 +860,4 @@ welcome.networks=شبکه‌ها
geo.country=کشور:
file.download-metadata=دانلود ابرداده
article.is.template=الگو است:
file.upload.metadata=آپلود فراداده
......@@ -860,3 +860,4 @@ welcome.networks=Réseaux
geo.country=Pays :
file.download-metadata=Télécharger les métadonnées
article.is.template=Est-ce que le modèle:
file.upload.metadata=Importer des métadonnées
......@@ -860,3 +860,4 @@ welcome.networks=Redes
geo.country=País:
file.download-metadata=Baixar metadados
article.is.template=É modelo:
file.upload.metadata=Carregar metadados
......@@ -858,5 +858,6 @@ welcome.search-genesys=Поиск по Genesys
welcome.networks=Сети
geo.country=Страна:
file.download-metadata=Загрузить метаданные
file.download-metadata=Скачать метаданные
article.is.template=Шаблон:
file.upload.metadata=Загрузить метаданные
......@@ -860,3 +860,4 @@ welcome.networks=网站
geo.country=国家/地区:
file.download-metadata=下載元數據
article.is.template=是模板
file.upload.metadata=下載
......@@ -81,26 +81,38 @@
</tbody>
</table>
<form style="padding-bottom: 5px;" action="<c:url value="/wiews/${wiewsCode}/files/download/metadata"><c:param name="${_csrf.parameterName}" value="${_csrf.token}" /></c:url>"
method="post" enctype="multipart/form-data" class="">
<input type="hidden" name="wiewsCode" value="${wiewsCode}"/>
<input type="hidden" name="repositoryPath" value="${currentPath}"/>
<button type="submit" class="btn btn-primary"><spring:message code="file.download-metadata"/></button>
</form>
<form action="<c:url value="/wiews/${wiewsCode}/upload-file"><c:param name="${_csrf.parameterName}" value="${_csrf.token}" /></c:url>"
method="post" enctype="multipart/form-data" class="">
<div class="row">
<div class="col-xs-6">
<form action="<c:url value='/wiews/${wiewsCode}/upload-file' />" method="post" enctype="multipart/form-data" class="">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<input type="hidden" name="repositoryPath" value="${currentPath}"/>
<div class="form-group">
<input type="file" name="file" class="upload-file"/>
<p class="help-block">
Pick a file to upload to current path <b><c:out value="${currentPath}"/></b>
</p>
</div>
<button type="submit" class="btn btn-primary upload-btn"><spring:message code="file.upload-file"/></button>
</form>
</div>
<div class="col-xs-6">
<form action="<c:url value='/wiews/${wiewsCode}/files/upload/metadata' />" method="post" enctype="multipart/form-data" class="">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="form-group">
<input type="file" name="file" class="upload-file-metadata"/>
<p class="help-block">
Pick a file to update the metadata.
</p>
</div>
<button type="submit" class="btn btn-primary upload-btn-metadata"><spring:message code="file.upload.metadata"/></button>
</form>
<input type="hidden" name="wiewsCode" value="${wiewsCode}" />
<input type="hidden" name="repositoryPath" value="${currentPath}" />
<div class="form-group">
<input type="file" name="file" />
<p class="help-block">
Pick a file to upload to current path <b><c:out value="${currentPath}" /></b>
</p>
</div>
<button type="submit" class="btn btn-primary"><spring:message code="file.upload-file" /></button>
</form>
<form style="margin-top: 5px;" method="post" action="<c:url value='/wiews/${wiewsCode}/files/download/metadata' />" class="">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<button type="submit" class="btn btn-primary"><spring:message code="file.download-metadata"/></button>
</form>
</div>
</div>
<content tag="javascript">
<script type="text/javascript">
......@@ -114,6 +126,26 @@
return false;
}
});
if ($('.upload-file').val() == "") {
$('.upload-btn').prop('disabled', true);
}
$('.upload-file').change(function () {
$('.upload-btn').prop(
'disabled',
!($('.upload-file').val()));
});
if ($('.upload-file-metadata').val() == "") {
$('.upload-btn-metadata').prop('disabled', true);
}
$('.upload-file-metadata').change(function () {
$('.upload-btn-metadata').prop(
'disabled',
!($('.upload-file-metadata').val()));
});
})
</script>
</content>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment