Skip to content

Commit

Permalink
resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Mar 4, 2021
2 parents f32dbe1 + b03f50f commit 2afff01
Show file tree
Hide file tree
Showing 38 changed files with 1,098 additions and 798 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>edu.tamu.weaver</groupId>
<artifactId>webservice-parent</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
</parent>

<modules>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.tamu.catalog.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface DefaultCatalog {
String value();
}
17 changes: 17 additions & 0 deletions service/src/main/java/edu/tamu/catalog/config/AppWebMvcConfig.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package edu.tamu.catalog.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import edu.tamu.catalog.resolver.CatalogServiceArgumentResolver;
import edu.tamu.catalog.service.CatalogService;

@EnableWebMvc
@Configuration
public class AppWebMvcConfig extends WebMvcConfigurerAdapter {

@Value("${app.security.allow-access}")
private String[] hosts;

@Lazy
@Autowired
private List<CatalogService> catalogServices;

@Override
public void addCorsMappings(CorsRegistry registry) {
// @formatter:off
Expand All @@ -24,4 +36,9 @@ public void addCorsMappings(CorsRegistry registry) {
// @formatter:on
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new CatalogServiceArgumentResolver(catalogServices));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package edu.tamu.catalog.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;

import edu.tamu.catalog.properties.CatalogServiceProperties;
import edu.tamu.catalog.properties.FolioProperties;
import edu.tamu.catalog.properties.VoyagerProperties;
import edu.tamu.catalog.service.CatalogService;
import edu.tamu.catalog.service.FolioCatalogService;
import edu.tamu.catalog.service.VoyagerCatalogService;

@Configuration
public class CatalogServiceConfig {

private static final String TYPE_FIELD = "type";

@Autowired
private ConfigurableListableBeanFactory beanFactory;

@Autowired
private ResourceLoader resourceLoader;

@Autowired
private ObjectMapper objectMapper;

@Bean
public List<CatalogService> createCatalogServices() throws IOException {
List<CatalogService> catalogServices = new ArrayList<>();
for (Resource resource : loadResources("classpath:/catalogs/*.json")) {
JsonNode propertiesNode = objectMapper.readTree(resource.getInputStream());
JsonNode typeNode = propertiesNode.get(TYPE_FIELD);
if (typeNode.isValueNode()) {
CatalogServiceFactory configServiceFactory = CatalogServiceFactory.valueOf(typeNode.asText().toUpperCase());
CatalogServiceProperties properties = (CatalogServiceProperties) objectMapper.treeToValue(propertiesNode, configServiceFactory.getPropertiesType());
CatalogService catalogService = configServiceFactory.build(properties);
beanFactory.initializeBean(catalogService, catalogService.getName());
beanFactory.autowireBean(catalogService);
beanFactory.registerSingleton(catalogService.getName(), catalogService);
catalogServices.add(catalogService);
}
}

return catalogServices;
}

private List<Resource> loadResources(String pattern) throws IOException {
return Arrays.asList(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern));
}

private interface CatalogServiceType {
public CatalogService build(CatalogServiceProperties properties);

public Class<?> getPropertiesType();
}

private enum CatalogServiceFactory implements CatalogServiceType {

VOYAGER {
@Override
public CatalogService build(CatalogServiceProperties properties) {
return new VoyagerCatalogService(properties);
}

@Override
public Class<?> getPropertiesType() {
return VoyagerProperties.class;
}
},

FOLIO {
@Override
public CatalogService build(CatalogServiceProperties properties) {
return new FolioCatalogService(properties);
}

@Override
public Class<?> getPropertiesType() {
return FolioProperties.class;
}
};

}

}
5 changes: 2 additions & 3 deletions service/src/main/java/edu/tamu/catalog/config/RestConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.tamu.catalog.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
Expand All @@ -9,8 +8,8 @@
public class RestConfig {

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
public RestTemplate restTemplate() {
return new RestTemplate();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,65 @@
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.JsonProcessingException;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;

import edu.tamu.catalog.annotation.DefaultCatalog;
import edu.tamu.catalog.domain.model.HoldingsRecord;
import edu.tamu.catalog.service.CatalogService;
import edu.tamu.catalog.service.CatalogServiceFactory;
import edu.tamu.weaver.response.ApiResponse;

@RestController
@RequestMapping("/catalog-access")
public class CatalogAccessController {

@Autowired
private CatalogServiceFactory catalogServiceFactory;

/**
* Provides the raw CatalogHolding data
*
* @param String catalogName (optional)
* @param CatalogService catalogService (resolved by query parameter catalogName)
* @param String bibId
* @return
* @throws JsonProcessingException
* @throws IOException
*/
@RequestMapping("/get-holdings")
public ApiResponse getHoldings(@RequestParam(value="catalogName", defaultValue="evans") String catalogName, @RequestParam("bibId") String bibId) {
List<HoldingsRecord> catalogHoldings = getCatalogServiceByName(catalogName).getHoldingsByBibId(bibId);
@RequestMapping("/get-holdings")
public ApiResponse getHoldings(
@DefaultCatalog("evans") CatalogService catalogService,
@RequestParam(required = true) String bibId
) {
List<HoldingsRecord> catalogHoldings = catalogService.getHoldingsByBibId(bibId);
if (catalogHoldings != null) {
return new ApiResponse(SUCCESS, catalogHoldings);
} else {
return new ApiResponse(ERROR,"Error retrieving holdings from " + catalogName + "catalog");
return new ApiResponse(ERROR, "Error retrieving holdings from " + catalogService.getName() + " catalog");
}
}
}

/**
* Provides data for a single CatalogHolding
*
* @param String catalogName (optional)
* @param CatalogService catalogService (resolved by query parameter catalogName)
* @param String bibId
* @param String holdingId
* @return
* @throws JsonProcessingException
* @throws IOException
*/
@RequestMapping("/get-holding")
public ApiResponse getHolding(@RequestParam(value="catalogName", defaultValue="evans") String catalogName, @RequestParam("bibId") String bibId, @RequestParam("holdingId") String holdingId) {
HoldingsRecord catalogHolding = getCatalogServiceByName(catalogName).getHolding(bibId, holdingId);
public ApiResponse getHolding(
@DefaultCatalog("evans") CatalogService catalogService,
@RequestParam(required = true) String bibId,
@RequestParam(required = true) String holdingId
) {
HoldingsRecord catalogHolding = catalogService.getHolding(bibId, holdingId);
if (catalogHolding != null) {
return new ApiResponse(SUCCESS, catalogHolding);
} else {
return new ApiResponse(ERROR,"Error retrieving holding from " + catalogName + "catalog");
return new ApiResponse(ERROR, "Error retrieving holding from " + catalogService.getName() + " catalog");
}
}

private CatalogService getCatalogServiceByName(String catalogName) {
return catalogServiceFactory.getOrCreateCatalogService(catalogName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,38 @@

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import edu.tamu.catalog.annotation.DefaultCatalog;
import edu.tamu.catalog.domain.model.FeesFines;
import edu.tamu.catalog.domain.model.LoanItem;
import edu.tamu.catalog.service.CatalogService;
import edu.tamu.catalog.service.CatalogServiceFactory;

@RestController
@RequestMapping("/patron")
public class PatronController {

@Autowired
private CatalogServiceFactory catalogServiceFactory;

/**
* Provides data for all fees and fines associated with a patron.
*
* @param String catalogName (optional)
* @param String user
* @param CatalogService catalogService (resolved by query parameter catalogName)
* @param String uin
* @return
* @throws Exception
*/
@GetMapping("/{uin}/fines")
public @ResponseBody ResponseEntity<FeesFines> fines(@RequestParam(value="catalogName", defaultValue="folio") String catalogName, @PathVariable String uin) throws Exception {
FeesFines feesFines = getCatalogServiceByName(catalogName).getFeesFines(uin);
public @ResponseBody ResponseEntity<FeesFines> fines(
@DefaultCatalog("folio") CatalogService catalogService,
@PathVariable(required = true) String uin
) throws Exception {
FeesFines feesFines = catalogService.getFeesFines(uin);

return ResponseEntity.status(HttpStatus.OK)
.body(feesFines);
Expand All @@ -44,32 +42,40 @@ public class PatronController {
/**
* Provides data for all loan items associated with a patron.
*
* @param String catalogName (optional)
* @param String user
* @param CatalogService catalogService (resolved by query parameter catalogName)
* @param String uin
* @return
*/
@GetMapping("/{uin}/loans")
public @ResponseBody ResponseEntity<List<LoanItem>> getLoanItems(@RequestParam(value="catalogName", defaultValue="folio") String catalogName, @PathVariable String uin) throws Exception {
List<LoanItem> loanItems = getCatalogServiceByName(catalogName).getLoanItems(uin);
public @ResponseBody ResponseEntity<List<LoanItem>> getLoanItems(
@DefaultCatalog("folio") CatalogService catalogService,
@PathVariable(required = true) String uin
) throws Exception {
List<LoanItem> loanItems = catalogService.getLoanItems(uin);

return ResponseEntity.status(HttpStatus.OK)
.body(loanItems);
.body(loanItems);
}

/**
* Renews a single loan item associated with a patron.
*
* @param String catalogName (optional)
* @param String user
* @param CatalogService catalogService (resolved by query parameter catalogName)
* @param String uin
* @param String itemId
* @return
*/
@PostMapping("/{uin}/renew/{itemId}")
public @ResponseBody ResponseEntity<LoanItem> renewItem(@RequestParam(value="catalogName", defaultValue="folio") String catalogName, @PathVariable String uin, @PathVariable String itemId) throws Exception {
LoanItem loanItem = getCatalogServiceByName(catalogName).renewItem(uin, itemId);
public @ResponseBody ResponseEntity<LoanItem> renewItem(
@DefaultCatalog("folio") CatalogService catalogService,
@PathVariable(required = true) String uin,
@PathVariable(required = true) String itemId
) throws Exception {
LoanItem loanItem = catalogService.renewItem(uin, itemId);

return ResponseEntity.status(HttpStatus.OK)
.body(loanItem);
}

private CatalogService getCatalogServiceByName(String catalogName) {
return catalogServiceFactory.getOrCreateCatalogService(catalogName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.tamu.catalog.properties;

import lombok.Data;

@Data
public abstract class AbstractCatalogServiceProperties implements CatalogServiceProperties {

private String type;

private String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.tamu.catalog.properties;

public interface CatalogServiceProperties {

public String getType();

public String getName();

}

0 comments on commit 2afff01

Please sign in to comment.