Commit af08f15e authored by Noel's avatar Noel
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+4 −0
Original line number Diff line number Diff line
.*
!.gitignore
!.gitlab-ci.yml
target

.gitlab-ci.yml

0 → 100644
+22 −0
Original line number Diff line number Diff line
stages:
  - build

maven-build:
  stage: build
  image: redmic/maven-gitlab
  variables:
    MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
  only:
    - branches
  cache:
    paths:
      - .m2/repository/
  script:
    - mvn deploy -B
  after_script:
    - rm -r .m2/repository/es
  artifacts:
    name: "$CI_PROJECT_NAME"
    expire_in: '6 months'
    paths:
      - target/*.jar

README.md

0 → 100644
+1 −0
Original line number Diff line number Diff line
[![pipeline status](https://git.redmic.net/redmic-server/view-lib/badges/dev/pipeline.svg)](https://git.redmic.net/redmic-server/view-lib/commits/dev)

pom.xml

0 → 100644
+75 −0
Original line number Diff line number Diff line
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<parent>
		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-parent</artifactId>
    	<version>2.0.0.RELEASE</version>
		<relativePath/>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<groupId>es.redmic.lib</groupId>
	<artifactId>view-lib</artifactId>
	<packaging>jar</packaging>
	<version>0.6.0</version>
	<name>view-lib</name>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<java.version>1.8</java.version>
		<!-- REDMIC -->
		<redmic.rest-lib.version>0.6.0</redmic.rest-lib.version>
		<redmic.models.version>0.6.0</redmic.models.version>
		<redmic.broker-lib.version>0.6.0</redmic.broker-lib.version>
		
		<!-- OTHERS -->
		<ma.glasnost.orika.core.version>1.5.2</ma.glasnost.orika.core.version>
		
		<!-- Environment variables -->
		<env.MAVEN_REPO_URL>https://artifactory.redmic.net/artifactory</env.MAVEN_REPO_URL>
	</properties>

	<dependencies>
		<dependency>
			<groupId>es.redmic.lib</groupId>
			<artifactId>rest-lib</artifactId>
			<version>${redmic.rest-lib.version}</version>
		</dependency>
		
		<dependency>
			<groupId>es.redmic.lib</groupId>
			<artifactId>models</artifactId>
			<version>${redmic.models.version}</version>
		</dependency>
		
		<dependency>
			<groupId>es.redmic.lib</groupId>
			<artifactId>broker-lib</artifactId>
			<version>${redmic.broker-lib.version}</version>
		</dependency>
		
		<dependency>
			<groupId>ma.glasnost.orika</groupId>
			<artifactId>orika-core</artifactId>
			<version>${ma.glasnost.orika.core.version}</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<distributionManagement>
		<repository>
			<id>central</id>
			<name>redmic-releases</name>
			<url>${env.MAVEN_REPO_URL}/libs-release-local</url>
		</repository>
		<snapshotRepository>
			<id>snapshots</id>
			<name>redmic-snapshots</name>
			<url>${env.MAVEN_REPO_URL}/libs-snapshot-local</url>
			<uniqueVersion>false</uniqueVersion>
		</snapshotRepository>
	</distributionManagement>
</project>
+136 −0
Original line number Diff line number Diff line
package es.redmic.viewlib.common.controller;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import es.redmic.brokerlib.avro.common.CommonDTO;
import es.redmic.exception.databinding.DTONotValidException;
import es.redmic.models.es.common.dto.ElasticSearchDTO;
import es.redmic.models.es.common.dto.SuperDTO;
import es.redmic.models.es.common.model.BaseES;
import es.redmic.models.es.common.query.dto.MgetDTO;
import es.redmic.models.es.common.query.dto.SimpleQueryDTO;
import es.redmic.viewlib.common.dto.MetaDTO;
import es.redmic.viewlib.common.service.IBaseService;

public abstract class RController<TModel extends BaseES<?>, TDTO extends CommonDTO, TQueryDTO extends SimpleQueryDTO> {

	protected Class<TDTO> typeOfTDTO;

	protected Class<TQueryDTO> typeOfTQueryDTO;

	private Set<String> fieldsExcludedOnQuery = new HashSet<String>();

	private Map<String, Object> fixedQuery = new HashMap<String, Object>();

	IBaseService<TModel, TDTO, TQueryDTO> service;

	public RController(IBaseService<TModel, TDTO, TQueryDTO> service) {
		this.service = service;
		defineTypeOfArguments();
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	public SuperDTO _search(@RequestParam(required = false, value = "fields") String[] fields,
			@RequestParam(required = false, value = "text") String text,
			@RequestParam(required = false, value = "from") Integer from,
			@RequestParam(required = false, value = "size") Integer size) {

		return new ElasticSearchDTO(service.find(fields, text, from, size, fixedQuery, fieldsExcludedOnQuery));
	}

	@RequestMapping(value = "/_search", method = RequestMethod.POST)
	@ResponseBody
	public SuperDTO _advancedSearch(@Valid @RequestBody TQueryDTO queryDTO, BindingResult bindingResult) {

		if (bindingResult != null && bindingResult.hasErrors())
			throw new DTONotValidException(bindingResult);

		return new ElasticSearchDTO(service.find(queryDTO, fixedQuery, fieldsExcludedOnQuery));
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public SuperDTO _get(@PathVariable("id") String id) {

		MetaDTO<?> response = service.findById(id);
		return new ElasticSearchDTO(response, response.get_source() == null ? 0 : 1);
	}

	@RequestMapping(value = "/_mget", method = RequestMethod.POST)
	@ResponseBody
	public SuperDTO _mget(@Valid @RequestBody MgetDTO dto, BindingResult errorDto) {

		if (errorDto.hasErrors())
			throw new DTONotValidException(errorDto);

		return new ElasticSearchDTO(service.mget(dto));
	}

	@RequestMapping(value = "/_suggest", method = RequestMethod.GET)
	@ResponseBody
	public SuperDTO _suggest(@RequestParam(required = false, value = "fields") String[] fields,
			@RequestParam("text") String text, @RequestParam(required = false, value = "size") Integer size) {

		return new ElasticSearchDTO(service.suggest(fields, text, size, fixedQuery, fieldsExcludedOnQuery));
	}

	@RequestMapping(value = "/_suggest", method = RequestMethod.POST)
	@ResponseBody
	public SuperDTO _advancedSuggest(@Valid @RequestBody TQueryDTO queryDTO, BindingResult bindingResult) {

		return new ElasticSearchDTO(service.suggest(queryDTO, fixedQuery, fieldsExcludedOnQuery));
	}

	@RequestMapping(value = { "${controller.mapping.FILTER_SCHEMA}" }, method = RequestMethod.GET)
	@ResponseBody
	public ElasticSearchDTO getFilterSchema() {

		return new ElasticSearchDTO(service.getFilterSchema(fieldsExcludedOnQuery), 1);
	}

	@SuppressWarnings("unchecked")
	private void defineTypeOfArguments() {

		int numberOfArguments = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments().length;
		Type[] arguments = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();

		switch (numberOfArguments) {
		case 4:
			this.typeOfTDTO = (Class<TDTO>) (arguments[2]);
			this.typeOfTQueryDTO = (Class<TQueryDTO>) (arguments[3]);
			break;
		case 3:
			this.typeOfTDTO = (Class<TDTO>) (arguments[1]);
			this.typeOfTQueryDTO = (Class<TQueryDTO>) (arguments[2]);
			break;
		}
	}

	protected void setFieldsExcludedOnQuery(Set<String> fieldsExcludedOnQuery) {
		this.fieldsExcludedOnQuery = fieldsExcludedOnQuery;
	}

	protected void setFixedQuery(Map<String, Object> fixedQuery) {
		this.fixedQuery.putAll(fixedQuery);
	}

	protected void setFixedQuery(String term, Object value) {
		fixedQuery.put(term, value);
	}
}