Commit 36fac598 authored by gourav.malhotra2's avatar gourav.malhotra2

changed auto-created project according to stock quote

parent f99d5c3d
Pipeline #2284 running with stages
......@@ -8,7 +8,7 @@ import org.springframework.context.annotation.PropertySources;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.altimetrik.ee.demo.service.ComponentDetailsService;
import com.altimetrik.ee.demo.service.StockDetailsService;
@EnableAsync
@EnableScheduling
......@@ -21,8 +21,8 @@ public class Application {
public static void main(String[] args) {
context = SpringApplication.run(Application.class, args);
context.getBean(ComponentDetailsService.class)
.createComponentDetails(context.getEnvironment().getProperty("spring.application.name"));
/*context.getBean(StockDetailsService.class)
.createComponentDetails(context.getEnvironment().getProperty("spring.application.name"));*/
}
}
package com.altimetrik.ee.demo.bean;
public class ComponentDetailsBean {
private String componentName;
private String componentIdentifier;
public ComponentDetailsBean() {
super();
}
public ComponentDetailsBean(String componentName, String componentIdentifier) {
super();
this.componentName = componentName;
this.componentIdentifier = componentIdentifier;
}
public String getComponentName() {
return componentName;
}
public void setComponentName(String componentName) {
this.componentName = componentName;
}
public String getComponentIdentifier() {
return componentIdentifier;
}
public void setComponentIdentifier(String componentIdentifier) {
this.componentIdentifier = componentIdentifier;
}
@Override
public String toString() {
return "ComponentDetailsBean [componentName=" + componentName + ", componentIdentifier=" + componentIdentifier
+ "]";
}
}
package com.altimetrik.ee.demo.bean;
import java.util.List;
public class PairedComponentDetailsBean extends ComponentDetailsBean {
private List<ComponentDetailsBean> pairedComponentDetails;
public PairedComponentDetailsBean() {
super();
}
public PairedComponentDetailsBean(final String componentName, final String componentIdentifier) {
super(componentName, componentIdentifier);
}
public PairedComponentDetailsBean(final String componentName, final String componentIdentifier,
final List<ComponentDetailsBean> pairedComponentDetails) {
super(componentName, componentIdentifier);
this.pairedComponentDetails = pairedComponentDetails;
}
public PairedComponentDetailsBean(final ComponentDetailsBean componentDetails,
final List<ComponentDetailsBean> pairedComponentDetails) {
super(componentDetails.getComponentName(), componentDetails.getComponentIdentifier());
this.pairedComponentDetails = pairedComponentDetails;
}
public List<ComponentDetailsBean> getPairedComponentDetails() {
return pairedComponentDetails;
}
public void setPairedComponentDetails(List<ComponentDetailsBean> pairedComponentDetails) {
this.pairedComponentDetails = pairedComponentDetails;
}
}
......@@ -6,15 +6,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.altimetrik.ee.demo.bean.PairedComponentDetailsBean;
import com.altimetrik.ee.demo.service.ComponentDetailsService;
import com.altimetrik.ee.demo.entity.Stock;
import com.altimetrik.ee.demo.service.StockDetailsService;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(value = "/service")
@RequestMapping(value = "/stockQuote")
public class ServiceController {
protected static Logger logger = LoggerFactory.getLogger(ServiceController.class.getName());
......@@ -23,12 +24,12 @@ public class ServiceController {
private String applicationName;
@Autowired
private ComponentDetailsService componentDetailsService;
private StockDetailsService detailsService;
@GetMapping(value = "/")
@ApiOperation(value = "Get service name and identifier", notes = "Get service details and its corresponding values for all paired services", response = PairedComponentDetailsBean.class)
public PairedComponentDetailsBean findAll() {
return componentDetailsService.findAll(this.applicationName);
@GetMapping(value = "/queryQuote")
public java.util.List<Stock> search(@RequestParam String symbols) {
return detailsService.getStockQuotes(symbols);
}
}
package com.altimetrik.ee.demo.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
@Entity
@Table(name = "component_details")
public class ComponentDetailsEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "component_name")
private String componentName;
@Column(name = "component_identifier")
private String componentIdentifier;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_date", nullable = false, updatable = false)
private Date createdDate;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_date")
private Date updatedDate;
public ComponentDetailsEntity() {
super();
}
public ComponentDetailsEntity(String componentName, String componentIdentifier) {
super();
this.componentName = componentName;
this.componentIdentifier = componentIdentifier;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getComponentName() {
return componentName;
}
public void setComponentName(String componentName) {
this.componentName = componentName;
}
public String getComponentIdentifier() {
return componentIdentifier;
}
public void setComponentIdentifier(String componentIdentifier) {
this.componentIdentifier = componentIdentifier;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((componentIdentifier == null) ? 0 : componentIdentifier.hashCode());
result = prime * result + ((componentName == null) ? 0 : componentName.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ComponentDetailsEntity other = (ComponentDetailsEntity) obj;
if (componentIdentifier == null) {
if (other.componentIdentifier != null)
return false;
} else if (!componentIdentifier.equals(other.componentIdentifier))
return false;
if (componentName == null) {
if (other.componentName != null)
return false;
} else if (!componentName.equals(other.componentName))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "ComponentDetailsEntity [id=" + id + ", componentName=" + componentName + ", componentIdentifier="
+ componentIdentifier + "]";
}
}
package com.altimetrik.ee.demo.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
@Entity
@Table(name = "stock")
public class Stock {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "stock_quote")
private String stockQuote;
@Column(name = "company_name")
private String companyName;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "stock_date", nullable = false, updatable = false)
private Date date;
@Column(name = "currency")
private String currency;
public String getStockQuote() {
return stockQuote;
}
public void setStockQuote(String stockQuote) {
this.stockQuote = stockQuote;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Column(name = "price")
private String price;
public Stock() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
package com.altimetrik.ee.demo.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.altimetrik.ee.demo.bean.ComponentDetailsBean;
import com.altimetrik.ee.demo.entity.ComponentDetailsEntity;
@Repository
public interface ComponentDetailsRepository extends JpaRepository<ComponentDetailsEntity, Long> {
<S extends ComponentDetailsEntity> S save(final S componentDetails);
<S extends ComponentDetailsEntity> List<S> save(final Iterable<S> componentDetails);
ComponentDetailsEntity findByComponentName(final String componentName);
ComponentDetailsEntity findByComponentIdentifier(final String componentIdentifier);
@Query("SELECT new com.altimetrik.ee.demo.bean.ComponentDetailsBean(c.componentName,c.componentIdentifier) FROM ComponentDetailsEntity c WHERE c.componentName = (:componentName)")
ComponentDetailsBean getByComponentName(@Param("componentName") final String componentName);
@Query("SELECT new com.altimetrik.ee.demo.bean.ComponentDetailsBean(c.componentName,c.componentIdentifier) FROM ComponentDetailsEntity c WHERE c.componentName != (:componentName)")
List<ComponentDetailsBean> getByComponentNameNotIn(@Param("componentName") final String componentName);
}
package com.altimetrik.ee.demo.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.altimetrik.ee.demo.entity.Stock;
@Repository
public interface StockDetailsRepository extends JpaRepository<Stock, Long> {
Stock findByCompanyName(final String componentName);
Stock findBystockQuote(final String stockQuote);
List<Stock> findByStockQuoteIn(List<String> quotes);
}
package com.altimetrik.ee.demo.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.altimetrik.ee.demo.bean.PairedComponentDetailsBean;
import com.altimetrik.ee.demo.entity.Stock;
@Service
public interface ComponentDetailsService {
boolean createComponentDetails(final String applicationName);
public interface StockDetailsService {
PairedComponentDetailsBean findAll(final String applicationName);
List<Stock> getStockQuotes(String quotesCSV);
}
package com.altimetrik.ee.demo.service.impl;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.altimetrik.ee.demo.bean.PairedComponentDetailsBean;
import com.altimetrik.ee.demo.entity.ComponentDetailsEntity;
import com.altimetrik.ee.demo.repository.ComponentDetailsRepository;
import com.altimetrik.ee.demo.service.ComponentDetailsService;
@Service
public class ComponentDetailsServiceImpl implements ComponentDetailsService {
protected static Logger logger = LoggerFactory.getLogger(ComponentDetailsServiceImpl.class.getName());
@Value("${spring.application.name}")
private String applicationName;
@Autowired
private ComponentDetailsRepository componentDetailsRepository;
@Override
public PairedComponentDetailsBean findAll(final String applicationName) {
final PairedComponentDetailsBean pairedComponentDetails = new PairedComponentDetailsBean(
this.componentDetailsRepository.getByComponentName(applicationName),
this.componentDetailsRepository.getByComponentNameNotIn(applicationName));
return pairedComponentDetails;
}
@Override
public boolean createComponentDetails(final String applicationName) {
if (this.componentDetailsRepository.findByComponentName(applicationName) == null) {
this.componentDetailsRepository
.save(new ComponentDetailsEntity(applicationName, UUID.randomUUID().toString()));
}
return true;
}
@Scheduled(cron = "${cron.component.identifier.reg-ex}")
public void regenerateComponentIdentifier() {
final ComponentDetailsEntity componentDetails = this.componentDetailsRepository
.findByComponentName(this.applicationName);
componentDetails.setComponentIdentifier(UUID.randomUUID().toString());
this.componentDetailsRepository.save(componentDetails);
}
}
package com.altimetrik.ee.demo.service.impl;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.altimetrik.ee.demo.entity.Stock;
import com.altimetrik.ee.demo.repository.StockDetailsRepository;
import com.altimetrik.ee.demo.service.StockDetailsService;
@Service
public class StockDetailsServiceImpl implements StockDetailsService {
protected static Logger logger = LoggerFactory.getLogger(StockDetailsServiceImpl.class.getName());
@Value("${spring.application.name}")
private String applicationName;
@Autowired
private StockDetailsRepository detailsRepository;
@Override
public List<Stock> getStockQuotes(String quotesCSV) {
return detailsRepository.findByStockQuoteIn(Arrays.asList(quotesCSV.split(",")));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment