Merge branches 'PAN-64' and 'master' of https://github.com/atusa17/ptp into PAN-64
This commit is contained in:
@@ -0,0 +1,186 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import edu.msudenver.tsp.services.dto.Definition;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DefinitionService {
|
||||
private final RestService restService;
|
||||
@Value("${persistence.api.connection.timeout.milliseconds}") private int connectionTimeoutMilliseconds;
|
||||
@Value("${persistence.api.socket.timeout.milliseconds}") private int socketTimeoutMilliseconds;
|
||||
@Value("${persistence.api.base.url}") private String persistenceApiBaseUrl;
|
||||
|
||||
@Autowired
|
||||
public DefinitionService(final RestService restService) {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
public Optional<List<Definition>> getAllDefinitions() {
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<List<Definition>> typeToken = new TypeToken<List<Definition>>(){};
|
||||
final Optional<List<Definition>> persistenceApiResponse =
|
||||
restService.get(persistenceApiBaseUrl + "definitions/",
|
||||
typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, null);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to get list of definitions");
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error getting list of definitions!", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Get all definitions request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Definition> findById(final int id) {
|
||||
if (id == 0) {
|
||||
LOG.error("Null id specified; returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
LOG.info("Sending request to find definition by id {}", id);
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Definition> typeToken = new TypeToken<Definition>(){};
|
||||
final Optional<Definition> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "definitions/" + id,
|
||||
typeToken, connectionTimeoutMilliseconds, socketTimeoutMilliseconds, null);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to find definition with id {}", id);
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error finding definition by id", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Find by id request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Definition> createDefinition(final Definition definition) {
|
||||
if (definition == null) {
|
||||
LOG.error("Given null definition, returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
LOG.info("Sending request to insert definition {}", definition);
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Definition> typeToken = new TypeToken<Definition>() {};
|
||||
final Optional<Definition> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "definitions/",
|
||||
new GsonBuilder().create().toJson(definition),
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Creation successful. Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to create new definition {}", definition);
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error creating new definition", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Create new definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Definition> updateDefinition(final Definition definition) {
|
||||
if (definition == null) {
|
||||
LOG.error("Given null definition, returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
if (definition.getId() == 0) {
|
||||
LOG.error("Given invalid id 0, returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
LOG.info("Sending request to update definition {}", definition);
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Definition> typeToken = new TypeToken<Definition>(){};
|
||||
final Optional<Definition> persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "definitions/" + definition.getId(),
|
||||
new GsonBuilder().create().toJson(definition),
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Update successful. Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to update definition {}", definition);
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error updating definition", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Update definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean deleteDefinition(final Definition definition) {
|
||||
if (definition == null) {
|
||||
LOG.error("Given null definition, returning false");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (definition.getId() == 0) {
|
||||
LOG.error("Given invalid id 0, returning false");
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG.info("Sending request to delete definition {}", definition);
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final boolean deleteIsSuccessful = restService.delete(persistenceApiBaseUrl + "definitions/" + definition.getId(),
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds,
|
||||
HttpStatus.NO_CONTENT);
|
||||
|
||||
if (deleteIsSuccessful) {
|
||||
LOG.info("Deletion successful. Returning true");
|
||||
} else {
|
||||
LOG.info("Unable to delete definition {}", definition);
|
||||
}
|
||||
|
||||
return deleteIsSuccessful;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error when deleting definition", e);
|
||||
return false;
|
||||
} finally {
|
||||
LOG.info("Delete definition request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package edu.msudenver.tsp.services;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration(exclude = {DevToolsDataSourceAutoConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class,
|
||||
DataSourceAutoConfiguration.class})
|
||||
public class ServiceConfig {
|
||||
}
|
||||
@@ -6,10 +6,12 @@ import edu.msudenver.tsp.services.dto.Account;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@@ -25,7 +27,96 @@ public class UserService {
|
||||
this.restService = restService;
|
||||
}
|
||||
|
||||
public Optional<Account> createNewAccount(final Account account) {
|
||||
public Optional<List<Account>> getListOfAccounts() {
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<List<Account>> typeToken = new TypeToken<List<Account>>() {};
|
||||
final Optional<List<Account>> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/",
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds,
|
||||
null);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.warn("Unable to get the list of accounts");
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error getting the list of accounts", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Get the list of accounts request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> findAccountById(final int id) {
|
||||
|
||||
if (id == 0) {
|
||||
LOG.error("No user ID specified! Returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
||||
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/id?id=" + id,
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds,
|
||||
null);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.warn("Unable to find account with id {}", id);
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error finding account by id", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Find account by ID request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> findAccountByUsername(final String username) {
|
||||
if (username == null) {
|
||||
LOG.error("No username specified! Returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
||||
final Optional<Account> persistenceApiResponse = restService.get(persistenceApiBaseUrl + "accounts/username?username=" + username,
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds,
|
||||
null);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.warn("Unable to GET account with username {}", username);
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error finding account by username", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Find account by username request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> createAccount(final Account account) {
|
||||
if (account == null) {
|
||||
LOG.error("Given null account, returning {}");
|
||||
return Optional.empty();
|
||||
@@ -34,8 +125,8 @@ public class UserService {
|
||||
|
||||
try {
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>() {};
|
||||
final Optional<Account> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "/accounts/",
|
||||
new GsonBuilder().create().toJson(account),
|
||||
final Optional<Account> persistenceApiResponse = restService.post(persistenceApiBaseUrl + "accounts/",
|
||||
new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account),
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
@@ -43,15 +134,88 @@ public class UserService {
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.info("Unable to create new account {}", account.toString());
|
||||
LOG.warn("Unable to create new account {}", account.toString());
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error creating new account {}", e);
|
||||
LOG.error("Error creating new account", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Create new account request took {} ms", Duration.between(start, Instant.now()).toMillis());
|
||||
LOG.info("Create new account request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Account> updateAccount(final Account account) {
|
||||
if (account == null) {
|
||||
LOG.error("Specified account is null; returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
if (account.getId() == 0) {
|
||||
LOG.error("No user ID specified! Returning {}");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final int id = account.getId();
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
final TypeToken<Account> typeToken = new TypeToken<Account>(){};
|
||||
final Optional<Account> persistenceApiResponse = restService.patch(persistenceApiBaseUrl + "accounts/" + id,
|
||||
new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create().toJson(account),
|
||||
typeToken,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds);
|
||||
|
||||
if (persistenceApiResponse.isPresent()) {
|
||||
LOG.info("Returning {}", persistenceApiResponse.get());
|
||||
} else {
|
||||
LOG.warn("Unable to update user with id {}", account.getId());
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
} catch (final Exception e) {
|
||||
LOG.error("Error updating user", e);
|
||||
return Optional.empty();
|
||||
} finally {
|
||||
LOG.info("Update user request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean deleteAccount(final Account account) {
|
||||
if (account == null){
|
||||
LOG.error("Specified account is null; returning {}");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (account.getId() == 0) {
|
||||
LOG.error("No user ID specified! Returning {}");
|
||||
return false;
|
||||
}
|
||||
|
||||
final int id = account.getId();
|
||||
final Instant start = Instant.now();
|
||||
|
||||
try {
|
||||
|
||||
final boolean persistenceApiResponse = restService.delete(persistenceApiBaseUrl + "accounts/" + id,
|
||||
connectionTimeoutMilliseconds,
|
||||
socketTimeoutMilliseconds, HttpStatus.NO_CONTENT);
|
||||
if (persistenceApiResponse) {
|
||||
LOG.info("Returning {}", persistenceApiResponse);
|
||||
}
|
||||
else {
|
||||
LOG.error("Unable to delete user {}", account);
|
||||
}
|
||||
|
||||
return persistenceApiResponse;
|
||||
}catch (final Exception e) {
|
||||
LOG.error("Error deleting user", e);
|
||||
return false;
|
||||
} finally {
|
||||
LOG.info("Delete user request took {}ms", Duration.between(start, Instant.now()).toMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
@@ -15,11 +14,10 @@ import java.util.Date;
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Account extends BaseDto implements Serializable {
|
||||
@NotBlank(groups = edu.msudenver.tsp.persistence.dto.Account.Insert.class, message = "A username must be specified") @Size(max = 50) private String username;
|
||||
@NotBlank(groups = edu.msudenver.tsp.persistence.dto.Account.Insert.class, message = "A password must be specified") @Size(max = 256) private String password;
|
||||
@NotNull @SerializedName("administrator_status") private boolean administratorStatus;
|
||||
@Size(max = 50) private String username;
|
||||
@Size(max = 256) private String password;
|
||||
@NotNull private boolean administrator;
|
||||
@Temporal(TemporalType.DATE) @SerializedName("last_login") private Date lastLogin;
|
||||
|
||||
private static final long serialVersionUID = 7095627971593953734L;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package edu.msudenver.tsp.services.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class Definition extends BaseDto implements Serializable {
|
||||
@NotBlank(groups = Insert.class, message = "A name must be specified")
|
||||
@Size(min = 1, max = 200, message = "Must be between 1 and 200 characters")
|
||||
private String name;
|
||||
@NotBlank(groups = Insert.class, message = "At least one (1) definition must be specified")
|
||||
private List<String> definition;
|
||||
private List<String> notation;
|
||||
|
||||
private static final long serialVersionUID = 3412178112996807691L;
|
||||
|
||||
public interface Insert {}
|
||||
}
|
||||
@@ -24,6 +24,6 @@ public class RequestFactory {
|
||||
}
|
||||
|
||||
public Request patch(final String uri, final String requestJson) {
|
||||
return StringUtils.isNotBlank(requestJson) ? Request.Put(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Patch(uri);
|
||||
return StringUtils.isNotBlank(requestJson) ? Request.Patch(uri).bodyString(requestJson, ContentType.APPLICATION_JSON) : Request.Patch(uri);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
persistence.api.connection.timeout.milliseconds = 5000
|
||||
persistence.api.socket.timeout.milliseconds = 10000
|
||||
persistence.api.base.url = http://localhost:8090/
|
||||
persistence.api.connection.timeout.milliseconds=5000
|
||||
persistence.api.socket.timeout.milliseconds=10000
|
||||
persistence.api.base.url=http://localhost:8090/
|
||||
Reference in New Issue
Block a user