FilesMetadataUpdate.java

/*
 * 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.genesys.server.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys.util.ImportFromCSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

/**
 * Update metadata of repository files.
 */
@Component
@Slf4j
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 Map<String, String> updateFromCsv(InputStream str, char separator, char quoteChar, char escapeChar) throws IOException {
		return uploadData(str, separator, quoteChar, escapeChar);
	}

	@Override
	protected void verifyHeaders(String[] headers) throws IOException {
		log.info("Got headers: {}", ArrayUtils.toString(headers));
		if (headers.length < CSV_HEADERS.length) { // There can be more columns in the data (like md5, sha, etc), but those are ignored
			throw new IOException("CSV header count mismatch. Found " + headers.length + " instead of " + CSV_HEADERS.length + " columns");
		}
		for (int i = 0; i < CSV_HEADERS.length; i++) {
			if (! StringUtils.equalsIgnoreCase(CSV_HEADERS[i], headers[i])) {
				throw new IOException("Header mismatch at position " + i + ". Found '" + headers[i] + "' instead of '" + CSV_HEADERS[i] + "'");
			}
		}
	}

	@Override
	protected RepositoryFile readRecord(String[] recordLine) throws IOException {

		if (log.isDebugEnabled()) {
			log.debug("Importing: {}", List.of(recordLine));
		}

		if (CSV_HEADERS.length > recordLine.length) { // Need minimum number of columns
			throw new IOException("CSV header count mismatch. Found: " + recordLine.length + " instead of " + CSV_HEADERS.length);
		}

		RepositoryFile repositoryFile = new RepositoryFile();
		repositoryFile.setUuid(UUID.fromString(recordLine[0]));
		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]);
		return repositoryFile;
	}

	@Override
	protected Map<String, String> updateRecords(List<RepositoryFile> batch) throws Exception {
		return repositoryService.updateMetadata(batch);
	}
}