spring configuration
This commit is contained in:
parent
a853b89ca4
commit
6ae004eb7e
|
@ -3,13 +3,17 @@ package eu.dnetlib.services.index;
|
|||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import eu.dnetlib.common.app.AbstractDnetApp;
|
||||
import eu.dnetlib.common.index.solr.SolrService;
|
||||
import eu.dnetlib.domain.index.IndexConfiguration;
|
||||
import eu.dnetlib.domain.index.IndexField;
|
||||
|
||||
@SpringBootApplication
|
||||
public class IndexManagerApplication extends AbstractDnetApp {
|
||||
@EntityScan(basePackageClasses = { IndexConfiguration.class, IndexField.class })
|
||||
public class MainApplication extends AbstractDnetApp {
|
||||
|
||||
@Value("${solr.urls}")
|
||||
private String[] solrUrls;
|
||||
|
@ -18,7 +22,7 @@ public class IndexManagerApplication extends AbstractDnetApp {
|
|||
private String zooServer;
|
||||
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(IndexManagerApplication.class, args);
|
||||
SpringApplication.run(MainApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
|
@ -36,13 +36,14 @@ public class ApiController extends DnetRestController {
|
|||
}
|
||||
|
||||
@GetMapping("/configurations")
|
||||
public List<IndexConfiguration> listConfigurations() {
|
||||
public List<String> listConfigurations() {
|
||||
return indexConfigurationService.listConfigurations();
|
||||
}
|
||||
|
||||
@PostMapping("/configurations")
|
||||
public IndexConfiguration saveConfiguration(@RequestBody final IndexConfiguration conf) throws DnetException {
|
||||
if (StringUtils.isBlank(conf.getId())) { throw new DnetException("Index Configuration ID is empty"); }
|
||||
|
||||
return indexConfigurationService.saveConfiguration(conf);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package eu.dnetlib.services.index.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import eu.dnetlib.domain.index.IndexField;
|
||||
import eu.dnetlib.domain.index.IndexFieldPK;
|
||||
|
||||
public interface IndexFieldRepository extends JpaRepository<IndexField, IndexFieldPK> {
|
||||
|
||||
void deleteByIndexId(String confId);
|
||||
|
||||
List<IndexField> findByIndexId(String confId);
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package eu.dnetlib.services.index.repository.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -9,31 +10,46 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
import eu.dnetlib.domain.index.IndexConfiguration;
|
||||
import eu.dnetlib.errors.InformationServiceException;
|
||||
import eu.dnetlib.services.index.repository.IndexConfigurationRepository;
|
||||
import eu.dnetlib.services.index.repository.IndexFieldRepository;
|
||||
|
||||
@Service
|
||||
public class IndexConfigurationService {
|
||||
|
||||
@Autowired
|
||||
private IndexConfigurationRepository repository;
|
||||
private IndexConfigurationRepository indexConfigurationRepository;
|
||||
|
||||
@Autowired
|
||||
private IndexFieldRepository indexFieldRepository;
|
||||
|
||||
@Transactional
|
||||
public List<IndexConfiguration> listConfigurations() {
|
||||
return repository.findAll();
|
||||
public List<String> listConfigurations() {
|
||||
return indexConfigurationRepository.findAll().stream().map(IndexConfiguration::getId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public IndexConfiguration getConfiguration(final String confId) throws InformationServiceException {
|
||||
return repository.findById(confId).orElseThrow(() -> new InformationServiceException("Index configuration not found"));
|
||||
final IndexConfiguration conf =
|
||||
indexConfigurationRepository.findById(confId).orElseThrow(() -> new InformationServiceException("Index configuration not found"));
|
||||
conf.setFields(indexFieldRepository.findByIndexId(confId));
|
||||
return conf;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public IndexConfiguration saveConfiguration(final IndexConfiguration conf) {
|
||||
return repository.save(conf);
|
||||
indexConfigurationRepository.save(conf);
|
||||
indexFieldRepository.deleteByIndexId(conf.getId());
|
||||
conf.getFields().forEach(f -> {
|
||||
f.setIndexId(conf.getId());
|
||||
indexFieldRepository.save(f);
|
||||
});
|
||||
|
||||
return conf;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteConfiguration(final String confId) {
|
||||
repository.deleteById(confId);
|
||||
indexFieldRepository.deleteByIndexId(confId);
|
||||
indexConfigurationRepository.deleteById(confId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import eu.dnetlib.domain.resource.ResourceType;
|
|||
import eu.dnetlib.domain.resource.SimpleResource;
|
||||
|
||||
@SpringBootApplication
|
||||
|
||||
@EntityScan(basePackageClasses = { ResourceType.class, SimpleResource.class })
|
||||
public class MainApplication extends AbstractDnetApp {
|
||||
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
package eu.dnetlib.domain.index;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.Transient;
|
||||
|
||||
@Entity
|
||||
@Table(name = "index_configurations")
|
||||
|
@ -33,8 +31,8 @@ public class IndexConfiguration implements Serializable {
|
|||
@Column(name = "n_replicas")
|
||||
private int numReplicas;
|
||||
|
||||
@OneToMany(mappedBy = "indexId", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private Set<IndexField> fields;
|
||||
@Transient
|
||||
private List<IndexField> fields;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
|
@ -52,11 +50,11 @@ public class IndexConfiguration implements Serializable {
|
|||
this.description = description;
|
||||
}
|
||||
|
||||
public Set<IndexField> getFields() {
|
||||
public List<IndexField> getFields() {
|
||||
return fields;
|
||||
}
|
||||
|
||||
public void setFields(final Set<IndexField> fields) {
|
||||
public void setFields(final List<IndexField> fields) {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,13 +9,13 @@ public class IndexFieldPK implements Serializable {
|
|||
|
||||
private String indexId;
|
||||
|
||||
private String field;
|
||||
private String name;
|
||||
|
||||
public IndexFieldPK() {}
|
||||
|
||||
public IndexFieldPK(final String indexId, final String field) {
|
||||
public IndexFieldPK(final String indexId, final String name) {
|
||||
this.indexId = indexId;
|
||||
this.field = field;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getIndexId() {
|
||||
|
@ -26,17 +26,17 @@ public class IndexFieldPK implements Serializable {
|
|||
this.indexId = indexId;
|
||||
}
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setField(final String field) {
|
||||
this.field = field;
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(field, indexId);
|
||||
return Objects.hash(name, indexId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -44,11 +44,11 @@ public class IndexFieldPK implements Serializable {
|
|||
if (this == obj) { return true; }
|
||||
if ((obj == null) || (getClass() != obj.getClass())) { return false; }
|
||||
final IndexFieldPK other = (IndexFieldPK) obj;
|
||||
return Objects.equals(field, other.field) && Objects.equals(indexId, other.indexId);
|
||||
return Objects.equals(name, other.name) && Objects.equals(indexId, other.indexId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IndexFieldPK [indexId=" + indexId + ", field=" + field + "]";
|
||||
return "IndexFieldPK [indexId=" + indexId + ", name=" + name + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
|||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
@ -34,7 +34,7 @@ public class SolrServiceTest {
|
|||
solr = new SolrService("localhost", "http://localhost:8983/solr");
|
||||
conf = new IndexConfiguration();
|
||||
conf.setId(TEST_INDEX);
|
||||
conf.setFields(new HashSet<>());
|
||||
conf.setFields(new ArrayList<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -64,7 +64,7 @@ public class SolrServiceTest {
|
|||
conf2.setNumReplicas(1);
|
||||
conf2.setNumShards(1);
|
||||
conf2.setSolrConfig("_default");
|
||||
conf2.setFields(new HashSet<>());
|
||||
conf2.setFields(new ArrayList<>());
|
||||
|
||||
assertFalse(solr.existsIndex(conf2.getId()));
|
||||
solr.createIndex(conf2);
|
||||
|
|
|
@ -57,6 +57,8 @@ services:
|
|||
- SPRING_DATASOURCE_URL=jdbc:postgresql://db-main:${PG_PORT}/${PG_INDEX_DB}
|
||||
- SPRING_DATASOURCE_USERNAME=${PG_USER}
|
||||
- SPRING_DATASOURCE_PASSWORD=${PG_PASSWORD}
|
||||
- SOLR_URLS=http://solr-1:${SOLR_PORT}/solr
|
||||
- ZOOKEEPER_SERVER=zoo-1
|
||||
profiles:
|
||||
- index
|
||||
|
||||
|
|
Loading…
Reference in New Issue