Commit 3d3a8f02 authored by Noel Alonso's avatar Noel Alonso
Browse files

Elimina generación de sitemap desde api

parent 2de8b559
Loading
Loading
Loading
Loading
+4 −19
Original line number Diff line number Diff line
@@ -22,49 +22,34 @@ package es.redmic.api.utils.sitemap.controller;

import java.io.File;
import java.io.FileInputStream;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import es.redmic.api.utils.sitemap.service.GenerateSitemapService;
import org.springframework.web.bind.annotation.GetMapping;
import es.redmic.exception.mediastorage.MSFileNotFoundException;
import es.redmic.mediastorage.service.MediaStorageServiceItfc;

@Controller
public class GenerateSitemapController {

	private GenerateSitemapService service;

	@Autowired
	MediaStorageServiceItfc mediaStorageService;

	@Value("${property.SITEMAP_DESTINATION_DIR}")
	private String PATH;

	@Autowired
	public GenerateSitemapController(GenerateSitemapService service) {
		this.service = service;
	}

	@PostConstruct
	public void GenerateSitemapControllerPostConstruct() {
		service.createSitemap();
	public GenerateSitemapController() {
		// TODO: eliminar servir sitemap desde la api
	}

	@RequestMapping(value = "/sitemap.xml", method = RequestMethod.GET, produces = { "text/xml", "application/xml" })
	@GetMapping(value = "/sitemap.xml", produces = { "text/xml", "application/xml" })
	public void download(HttpServletResponse response) {

		String name = "sitemap.xml";

		// TODO: Esta funcionalidad es provisional. En caso de mantener el servir el xml
		// desde api, añadir esta funcionalidad al servicio.
		File file = mediaStorageService.openTempFile(PATH, name);

		response.reset();
+0 −45
Original line number Diff line number Diff line
package es.redmic.api.utils.sitemap.dto;

/*-
 * #%L
 * API
 * %%
 * 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;

public class ModuleNamesDTO extends ArrayList<String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String getParentName() {
		
		if (this.size() < 2)
			return null;
		return this.get(0);
	}
	
	public String getModuleName() {
		
		if (this.size() < 2)
			return null;
		return this.get(1);
	}
}
+0 −32
Original line number Diff line number Diff line
package es.redmic.api.utils.sitemap.dto;

/*-
 * #%L
 * API
 * %%
 * 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;

public class OpenModules extends ArrayList<ModuleNamesDTO> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

}
+0 −173
Original line number Diff line number Diff line
package es.redmic.api.utils.sitemap.service;

/*-
 * #%L
 * API
 * %%
 * 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.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import es.redmic.api.utils.sitemap.dto.ModuleNamesDTO;
import es.redmic.api.utils.sitemap.dto.OpenModules;
import es.redmic.es.common.queryFactory.geodata.AreaQueryUtils;
import es.redmic.es.common.queryFactory.geodata.CitationQueryUtils;
import es.redmic.es.common.queryFactory.geodata.PlatformTrackingQueryUtils;
import es.redmic.es.common.queryFactory.geodata.TrackingQueryUtils;
import es.redmic.es.common.service.MetaDataESService;
import es.redmic.mediastorage.service.FileUtils;
import es.redmic.models.es.common.query.dto.DataQueryDTO;
import es.redmic.utils.httpclient.HttpClient;
import es.redmic.utils.sitemap.Sitemap;

@Service
public class GenerateSitemapService {

	HttpClient client = new HttpClient();

	@Value("${property.URL_OPEN_MODULES}")
	private String openModulesUrl;

	@Autowired
	ApplicationContext ctx;

	@Value("${property.SITEMAP_BASE_URL}")
	private String BASE_URL;

	@Value("${property.SITEMAP_DESTINATION_DIR}")
	private String DESTINATION_DIR;

	@Value("#{'${property.LANGS}'.split(',')}")
	private List<String> langs;

	@Value("${property.DEFAULT_LANG}")
	private String defaultLang = "es";

	// @formatter:off

	private final static String ID_PROPERTY = "id",
			ID_PATTERN = "{id}",
			SERVICE_SUFFIX = "ESService";

	// @formatter:on

	private OpenModules openModules;

	@PostConstruct
	private void init() {

		openModules = (OpenModules) client.get(openModulesUrl, OpenModules.class);
	}

	public void createSitemap() {

		FileUtils.createDirectoryIfNotExist(DESTINATION_DIR);

		Sitemap sitemap = new Sitemap(BASE_URL, DESTINATION_DIR, langs, defaultLang);

		for (ModuleNamesDTO moduleNames : openModules) {

			String parentName = moduleNames.getParentName(), moduleName = moduleNames.getModuleName();

			if (!moduleName.contains(ID_PATTERN)) {
				sitemap.addUrl(createUrl(parentName, moduleName));
			} else {
				String serviceName = getServiceName(moduleName);
				if (serviceName == null) {
					System.out.println("Nombre del servicio no soportado en el generador de sitemap " + serviceName);
				} else {
					List<String> ids = getIds(serviceName, getQuery(moduleName));

					if (ids != null) {
						for (String id : ids) {
							String moduleNameWithId = moduleName.replace(ID_PATTERN, id);
							sitemap.addUrl(createUrl(parentName, moduleNameWithId));
						}
					}
				}
			}
		}

		sitemap.writeSitemap();
	}

	private String createUrl(String parentName, String moduleName) {

		return parentName != null ? parentName + "/" + moduleName : moduleName;
	}

	private String getServiceName(String moduleName) {

		String[] moduleNameSplit = moduleName.split("-");

		String name = null;

		if (moduleNameSplit.length > 1) {

			// TODO: extender funcionalidad para crear entradas de otros tipo de datos
			// getAllIds solo está implementado para metadata
			if (moduleName.contains("real-time-dashboard"))
				return null;

			return moduleNameSplit[0] + SERVICE_SUFFIX;
		}
		return name;

	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List<String> getIds(String serviceName, DataQueryDTO queryDTO) {

		MetaDataESService service = null;

		try {
			service = (MetaDataESService) ctx.getBean(serviceName);
		} catch (Exception e) {
			System.out.println("Nombre del servicio no soportado en el generador de sitemap " + serviceName);
			return new ArrayList<>();
		}

		return service.getAllIds(queryDTO, ID_PROPERTY);
	}

	private DataQueryDTO getQuery(String moduleName) {

		DataQueryDTO queryDTO = new DataQueryDTO();
		queryDTO.setAccessibilityIds(Arrays.asList(2L));

		if (moduleName.contains("map")) {
			queryDTO.setTerms(CitationQueryUtils.getActivityCategoryTermQuery());
		} else if (moduleName.contains("tracking")) {
			queryDTO.setTerms(TrackingQueryUtils.getActivityCategoryTermQuery());
		} else if (moduleName.contains("infrastructure")) {
			queryDTO.setTerms(PlatformTrackingQueryUtils.getActivityCategoryTermQuery());
		} else if (moduleName.contains("area")) {
			queryDTO.setTerms(AreaQueryUtils.getActivityCategoryTermQuery());
		}

		return queryDTO;
	}
}
+1 −24
Original line number Diff line number Diff line
@@ -44,10 +44,7 @@ import org.springframework.web.context.WebApplicationContext;

import es.redmic.ApiApplication;
import es.redmic.api.utils.sitemap.controller.GenerateSitemapController;
import es.redmic.api.utils.sitemap.dto.OpenModules;
import es.redmic.api.utils.sitemap.service.GenerateSitemapService;
import es.redmic.test.integration.ApiApplicationTest;
import es.redmic.test.integration.utils.JsonToBeanTestUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { ApiApplication.class,
@@ -58,9 +55,6 @@ public class GenerateSitemapTest {
	@MockBean
	GenerateSitemapController controller;

	@Autowired
	GenerateSitemapService service;

	@Autowired
	protected WebApplicationContext webApplicationContext;

@@ -72,8 +66,7 @@ public class GenerateSitemapTest {
	// @formatter:off

	private final String FILE_NAME = "sitemap.xml",
			GENERATE_SITEMAP_URL = "/" + FILE_NAME,
			OPEN_MODULES_RESOURCE = "/sitemap/openModules.json";
			GENERATE_SITEMAP_URL = "/" + FILE_NAME;

	// @formatter:on

@@ -92,20 +85,4 @@ public class GenerateSitemapTest {

		mockMvc.perform(get(GENERATE_SITEMAP_URL)).andExpect(status().isOk());
	}

	@Test
	public void generateSitemap_CreateFile_IfThereAreOpenModules() throws Exception {

		File f = new File(DESTINATION_DIR + "/" + FILE_NAME);
		f.delete();

		OpenModules openModules = (OpenModules) JsonToBeanTestUtil.getBean(OPEN_MODULES_RESOURCE, OpenModules.class);

		Whitebox.setInternalState(service, "openModules", openModules);
		service.createSitemap();

		assertTrue(f.exists() && f.isFile());

		// TODO: Cuando tenga la base de datos de tests, comprobar contenido.
	}
}