Commit 9552501c authored by Noel Alonso's avatar Noel Alonso
Browse files

Elimina funcionalidades deprecated + tests

parent 584269bd
Loading
Loading
Loading
Loading
+0 −54
Original line number Diff line number Diff line
package es.redmic.tasks.config;

/*-
 * #%L
 * Tasks
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import es.redmic.es.config.EsClientProvider;
import es.redmic.es.config.EsConfig;

@Configuration
public class ElasticsearchConfiguration {

	@Value("#{'${elastic.addresses}'.split(',')}")
	private List<String> addresses;
	@Value("${elastic.port}")
	private Integer port;
	@Value("${elastic.clusterName}")
	private String clusterName;
	@Value("${elastic.xpackSecurityUser}")
	private String xpackSecurityUser;

	@Bean
	public EsClientProvider esClientProvider() {

		EsConfig elastic = new EsConfig();
		elastic.setAddresses(addresses);
		elastic.setPort(port);
		elastic.setClusterName(clusterName);
		elastic.setXpackSecurityUser(xpackSecurityUser);
		return new EsClientProvider(elastic);
	}
}
+0 −82
Original line number Diff line number Diff line
package es.redmic.tasks.ingest.geodata.area.controller;

/*-
 * #%L
 * Tasks
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import org.springframework.batch.core.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Controller;

import es.redmic.brokerlib.avro.common.MessageWrapper;
import es.redmic.brokerlib.utils.MessageWrapperUtils;
import es.redmic.exception.common.ExceptionType;
import es.redmic.exception.common.InternalException;
import es.redmic.tasks.common.controller.TaskJobBaseWithInterventionController;
import es.redmic.tasks.ingest.geodata.area.service.IngestDataAreaService;
import es.redmic.tasks.ingest.model.geodata.area.dto.RunTaskIngestDataAreaDTO;
import es.redmic.tasks.ingest.model.matching.area.dto.AreaMatching;

@Controller
public class IngestDataAreaController extends TaskJobBaseWithInterventionController<AreaMatching> {

	@Value("${property.INGEST_DATA_AREA_TASK_NAME}")
	private String TASK_NAME;

	@Autowired
	public IngestDataAreaController(IngestDataAreaService service,
			@Autowired @Qualifier("createAreaIndexing") Job createAreaIndexing) {
		super(service);
		service.setIndexingJob(createAreaIndexing);
	}

	@KafkaListener(topics = "${broker.topic.task.ingest.area.run}")
	public void run(MessageWrapper payload) {

		logger.info("Arrancar tarea de ingesta de areas. User: " + payload.getUserId());

		RunTaskIngestDataAreaDTO dto = objectMapper.convertValue(
				MessageWrapperUtils.getMessageFromMessageWrapper(payload), RunTaskIngestDataAreaDTO.class);
		super.register(dto, payload.getUserId(), TASK_NAME);
	}

	@KafkaListener(topics = "${broker.topic.task.ingest.area.resume}")
	public void resume(MessageWrapper payload) {

		logger.info("Intervención del usuario para ingesta de areas. User: " + payload.getUserId() + " task: "
				+ payload.getActionId());

		String taskId = payload.getActionId();

		// TODO: hacer excepción específica
		if (taskId == null)
			throw new InternalException(ExceptionType.INTERNAL_EXCEPTION);

		super.resume(taskId, objectMapper.convertValue(MessageWrapperUtils.getMessageFromMessageWrapper(payload),
				AreaMatching.class));
	}

	@Override
	protected boolean chkEventIsMine(String taskName) {
		return taskName.equals(TASK_NAME);
	}
}
+0 −132
Original line number Diff line number Diff line
package es.redmic.tasks.ingest.geodata.area.jobs;

/*-
 * #%L
 * Tasks
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.opengis.feature.simple.SimpleFeature;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.PrecisionModel;

import es.redmic.exception.tasks.ingest.IngestMatchingException;
import es.redmic.exception.tasks.ingest.IngestMatchingGeometryException;
import es.redmic.models.es.geojson.area.dto.AreaDTO;
import es.redmic.models.es.geojson.area.dto.AreaPropertiesDTO;
import es.redmic.tasks.ingest.geodata.common.jobs.shapefile.ShapeFileFieldSetMapper;
import es.redmic.tasks.ingest.model.matching.common.dto.ItemAreaTypeDTO;
import es.redmic.tasks.ingest.model.matching.common.dto.ItemCodeNullableDTO;
import es.redmic.tasks.ingest.model.matching.common.dto.ItemCommonDTO;
import es.redmic.tasks.ingest.model.matching.common.dto.ItemNameDTO;
import es.redmic.tasks.ingest.model.matching.common.dto.ItemRemarkDTO;

public class AreaFieldSetMapper implements ShapeFileFieldSetMapper<List<AreaDTO>> {

	private AreaParameters jobParameters;

	public AreaFieldSetMapper(AreaParameters jobParameters) {
		this.jobParameters = jobParameters;
	}

	@Override
	public List<AreaDTO> mapFieldSet(SimpleFeature fieldSet) {

		List<AreaDTO> areaDTOList = new ArrayList<AreaDTO>();

		AreaDTO commonDTO = getCommonDTO(fieldSet, jobParameters.getMatching().getItemsCommon());

		commonDTO.setGeometry(getGeometryFromFieldSet(fieldSet.getDefaultGeometry()));

		ItemNameDTO itemName = jobParameters.getMatching().getName();

		if (itemName != null)
			commonDTO.getProperties().setName(itemName.returnValue(fieldSet));

		ItemCodeNullableDTO itemCode = jobParameters.getMatching().getCode();

		if (itemCode != null)
			commonDTO.getProperties().setCode(itemCode.returnValue(fieldSet));

		ItemRemarkDTO itemRemark = jobParameters.getMatching().getRemark();

		if (itemRemark != null)
			commonDTO.getProperties().setRemark(itemRemark.returnValue(fieldSet));

		ItemAreaTypeDTO itemAreaType = jobParameters.getMatching().getAreaType();
		if (itemAreaType != null)
			commonDTO.getProperties().setAreaType(itemAreaType.returnValue(fieldSet));

		areaDTOList.add(commonDTO);

		return areaDTOList;
	}

	private MultiPolygon getGeometryFromFieldSet(Object objectGeometry) {

		if (objectGeometry == null)
			throw new IngestMatchingGeometryException("null", "Multipolygon | Polygon", jobParameters.getTaskId());

		if (objectGeometry instanceof MultiPolygon) {
			return (MultiPolygon) objectGeometry;
		} else if (objectGeometry instanceof Polygon) {
			GeometryFactory geomFactory = new GeometryFactory(new PrecisionModel(), 4326);
			List<Polygon> list = new ArrayList<Polygon>();
			list.add((Polygon) objectGeometry);
			Polygon[] polygonArray = GeometryFactory.toPolygonArray(list);
			return geomFactory.createMultiPolygon(polygonArray);
		} else {
			throw new IngestMatchingGeometryException(((Geometry) objectGeometry).getGeometryType(),
					"Multipolygon | Polygon", jobParameters.getTaskId());
		}
	}

	private AreaDTO getCommonDTO(SimpleFeature fieldSet, List<ItemCommonDTO> commonProps) {

		AreaDTO commonDTO = new AreaDTO();

		AreaPropertiesDTO properties = new AreaPropertiesDTO();

		properties.setActivityId(jobParameters.getActivityId());

		try {
			// @formatter:off
			for (ItemCommonDTO match : commonProps) {
		    	Object value = match.returnValue(fieldSet);
		    	if (value instanceof String) {
		    		String str = (String) value;
		    		if (str.length() == 0)
		    			value = null;
		    	}
				PropertyUtils.setProperty(properties, match.getField(), value);
				// @formatter:on
			}
		} catch (Exception e) {
			throw new IngestMatchingException(jobParameters.getTaskId(), e);
		}
		commonDTO.setProperties(properties);
		return commonDTO;
	}
}
+0 −52
Original line number Diff line number Diff line
package es.redmic.tasks.ingest.geodata.area.jobs;

/*-
 * #%L
 * Tasks
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import java.util.List;

import org.springframework.batch.item.ItemWriter;

import es.redmic.db.geodata.area.service.AreaService;
import es.redmic.exception.tasks.ingest.IngestPersistenceDataException;
import es.redmic.models.es.geojson.area.dto.AreaDTO;

public class AreaItemWritter implements ItemWriter<List<AreaDTO>> {

	AreaService service;

	private String taskId;

	public AreaItemWritter(AreaService service, String taskId) {

		this.service = service;
		this.taskId = taskId;
	}

	@Override
	public void write(List<? extends List<AreaDTO>> rows) {

		try {
			rows.forEach(row -> row.forEach(item -> service.save(item)));
		} catch (Exception e) {
			throw new IngestPersistenceDataException(taskId, e);
		}
	}
}
+0 −65
Original line number Diff line number Diff line
package es.redmic.tasks.ingest.geodata.area.jobs;

/*-
 * #%L
 * Tasks
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import es.redmic.db.geodata.area.service.AreaService;
import es.redmic.tasks.common.utils.JobUtils;
import es.redmic.tasks.ingest.common.jobs.MatchingParametersValidator;

@Component
@Transactional
public class AreaMatchingParametersValidator extends MatchingParametersValidator {

	@Autowired
	AreaService areaService;

	public AreaMatchingParametersValidator() {
	}

	public void validate(JobParameters jobParameters) {

		JobParameter parameters = jobParameters.getParameters().get(JOB_PARAMETER_KEY);
		AreaParameters areaParameters = JobUtils.JobParameters2UserParameters(parameters, AreaParameters.class);

		checkConstraints(areaParameters);
		beforeResumeJob(areaParameters);
	}

	protected void checkConstraints(AreaParameters areaParameters) {

		checkDataBindingConstraints(areaParameters);
		checkHeader(areaParameters.getTaskId(), areaParameters.getHeader(),
				areaParameters.getMatching().getMatchingColumns());
		checkFileConstraints(areaParameters.getTaskId(), areaParameters.getHeader(),
				areaParameters.getMatching().getMatchingColumns());
	}

	public void beforeResumeJob(AreaParameters areaParameters) {

		areaService.checkDataType(areaParameters.getActivityId());
	}
}
Loading