FirstRunListener.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.component.listener;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.service.CropService;
import org.genesys.server.service.GenesysService;
import org.genesys.server.service.GeoRegionService;
import org.genesys.server.service.GeoService;
import org.genesys.server.service.InstituteService;
import org.genesys.server.service.worker.AccessionUploader;
import org.genesys.server.service.worker.InstituteUpdater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
 * Import country data and WIEWS institutes if none exist at startup
 *
 * @author mobreza
 */
@Component
public class FirstRunListener extends RunAsAdminListener {

	@Value("${auto.createContent}")
	// Must **not** be final!
	private boolean createContent;

	@Autowired
	private GeoService geoService;

	@Autowired
	private InstituteService instituteService;

	@Autowired
	private InstituteUpdater instituteUpdater;

	@Autowired
	private GeoRegionService geoRegionService;

	@Autowired
	private CropService cropService;

	@Autowired
	private AccessionUploader uploader;

	@Autowired
	private GenesysService genesysService;

	@Override
	protected void init() throws Exception {
		if (!createContent) {
			LOG.warn("Skipping content creation on startup.");
			return;
		}

		if (geoService.listAll().size() == 0) {
			LOG.warn("No country data found. Loading");
			geoService.updateCountryData();
		}

		if (instituteService.list(PageRequest.of(0, 1)).getTotalElements() == 0) {
			LOG.warn("No WIEWS institutes found. Loading.");
			instituteUpdater.updateFaoInstitutes();
		}

		if (geoRegionService.findAll().isEmpty()) {
			LOG.warn("No geoRegion data found. Loading");
			geoRegionService.updateGeoRegionData();
		}

		if (cropService.listCrops().isEmpty()) {
			LOG.warn("No crops data found. Adding");

			addCrop("sweetpotato");
			addCrop("sorghum");
		}

		addSomeAccessions();
	}

	private void addCrop(String shortName) {
		LOG.info("No crops data found. Adding {}", shortName);
		cropService.addCrop(shortName, StringUtils.capitalize(shortName), StringUtils.capitalize(shortName));
	}

	private void addSomeAccessions() throws IOException {
		LOG.warn("Adding some passport data");

		final ClassLoader classLoader = CreateContentListener.class.getClassLoader();
		final PathMatchingResourcePatternResolver rpr = new PathMatchingResourcePatternResolver(classLoader);
		final String resourcePath = "/dummy-data/*";
		final Resource[] rs = rpr.getResources(resourcePath);
		for (final Resource r : rs) {
			final String instCode = r.getFilename().substring(0, r.getFilename().lastIndexOf(".json"));
			LOG.info("Using {} for instCode={}", r.getFilename(), instCode);

			final ObjectMapper mapper = new ObjectMapper();
			try (InputStream stream = r.getInputStream()) {
				final ArrayNode updates = (ArrayNode) mapper.readTree(stream);

				FaoInstitute institute = instituteService.getInstitute(instCode);
				if (institute == null) {
					throw new Exception("No Institute with instCode=" + instCode);
				}

				if (updates.size() > 0) {
					LOG.warn("Upserting {} accession records for {}", updates.size(), instCode);
					uploader.upsertAccessions(institute, updates);
					genesysService.updateAccessionCount(institute);
				}

			} catch (JsonParseException e) {
				LOG.error("Could not read dummy data for {} from {}. JSON is not valid.", instCode, r.getFilename(), e);
			} catch (Exception e) {
				LOG.error("Could not create dummy data. {}", e.getMessage(), e);
			}
		}
	}
}