Skip to content

Commit

Permalink
fix sonar type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
evaggelos99 committed May 24, 2024
1 parent fdf5d56 commit 728b8d8
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
public abstract class AbstractDomainObject {

final protected UUID uuid;
final protected Instant createdAt;
final protected Instant lastUpdated;
private final UUID uuid;
private final Instant createdAt;
private final Instant lastUpdated;

public AbstractDomainObject(final UUID uuid,
final Instant createdAt,
final Instant lastUpdated) {
protected AbstractDomainObject(final UUID uuid,
final Instant createdAt,
final Instant lastUpdated) {

this.uuid = uuid;
this.createdAt = createdAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

private static final String TIME_STAMP = "timeStamp";

/**
* Handle JSON responses that are not valid
*
Expand All @@ -42,7 +44,7 @@ Object>> handleMethodArgumentNotValidException(final MethodArgumentNotValidExcep
final String message = fieldError.getDefaultMessage();

final Map<String, Object> errorResponse = new HashMap<>();
errorResponse.put("timeStamp", Instant.now());
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("status", statusCode.value());
errorResponse.put("error",
String.format("The field: '%s' %s. Does not accept value: %s", field, message, valueGiven));
Expand All @@ -65,7 +67,7 @@ Map<String, Object>> handleObjectNotFoundException(final ObjectNotFoundException
final HttpStatusCode statusCode = exception.getStatusCode();

final Map<String, Object> errorResponse = new HashMap<>();
errorResponse.put("timeStamp", Instant.now());
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("status", statusCode.value());
errorResponse.put("error", String.format("The object of class: '%s' with ID: '%s' cannot be found",
exception.getClassOfObject(), exception.getUuid()));
Expand All @@ -81,7 +83,7 @@ Map<String, Object>> handleConstraintViolationException(final ConstraintViolatio
final Set<ConstraintViolation<?>> set = ex.getConstraintViolations();

final Map<String, Object> errorResponse = new HashMap<>();
errorResponse.put("timeStamp", Instant.now());
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("errors", set);

return new ResponseEntity<>(errorResponse, new HttpHeaders(), 400);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import java.util.Optional;
import java.util.UUID;

public interface IRepository<T, T_DTO> {
public interface IRepository<T, DTO> {

/**
*
* @param dto
* @return
*/
T save(T_DTO dto);
T save(DTO dto);

Optional<T> findById(UUID uuid);

Expand All @@ -21,6 +21,6 @@ public interface IRepository<T, T_DTO> {

Collection<T> findAll();

T edit(T_DTO dto);
T edit(DTO dto);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
//@ControllerAdvice
public class PostgresExceptionHandler {

private static final String TIME_STAMP = "timeStamp";
private static final String MESSAGE = "message";
private static final Logger LOGGER = LoggerFactory.getLogger(PostgresExceptionHandler.class);

@ExceptionHandler(PSQLException.class)
public ResponseEntity<Map<String, Object>> handlePSQLExceptionHandler(final PSQLException exc) {

final Map<String, Object> errorResponse = new HashMap<>();

errorResponse.put("message", exc.getMessage());
errorResponse.put(MESSAGE, exc.getMessage());
errorResponse.put(TIME_STAMP, Instant.now());

return new ResponseEntity<>(errorResponse, new HttpHeaders(), 500);

Expand All @@ -41,8 +44,8 @@ public ResponseEntity<Map<String, Object>> handleDuplicateKeyException(final Dup

final Map<String, Object> errorResponse = new HashMap<>();

errorResponse.put("message", "The uuid key that was provided already exists.");
errorResponse.put("timeStamp", Instant.now());
errorResponse.put(MESSAGE, "The uuid key that was provided already exists.");
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("status", 409);

LOGGER.trace("Duplicate key found: ", exc);
Expand All @@ -57,8 +60,8 @@ Map<String, Object>> handleDataIntegrityViolationException(final DataIntegrityVi

final Map<String, Object> errorResponse = new HashMap<>();

errorResponse.put("message", exc.getMessage());
errorResponse.put("timeStamp", Instant.now());
errorResponse.put(MESSAGE, exc.getMessage());
errorResponse.put(TIME_STAMP, Instant.now());
errorResponse.put("status", 500);

return new ResponseEntity<>(errorResponse, new HttpHeaders(), 500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public boolean deleteById(final UUID uuid) {
final int rows = this.jdbcTemplate
.update(this.attendeeQueriesProperties.getProperty(CrudQueriesOperations.DELETE_ID.name()), uuid);

final boolean deleted = rows == 1 ? true : false;
final boolean deleted = rows == 1;

if (deleted) {

Expand Down Expand Up @@ -163,7 +163,7 @@ private Attendee saveAttendee(final AttendeeDto attendee) {
private Attendee editAttendee(final AttendeeDto attendee) {

final UUID uuid = attendee.uuid();
final Timestamp createdAt = Timestamp.from(this.getAttendee(uuid).getCreatedAt());
final Timestamp createdAt = this.getCreatedAt(uuid);
final Timestamp timestamp = Timestamp.from(Instant.now());
final List<UUID> ticketIds = attendee.ticketIDs() != null ? attendee.ticketIDs() : List.of();
final String firstName = attendee.firstName();
Expand Down Expand Up @@ -195,9 +195,10 @@ private UUID[] convertToArray(final List<UUID> ticketIds) {

}

private AbstractDomainObject getAttendee(final UUID uuid) {
private Timestamp getCreatedAt(final UUID uuid) {

return this.findById(uuid).get();
return Timestamp.from(this.findById(uuid).map(AbstractDomainObject::getCreatedAt).orElse(Instant.now()));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public boolean deleteById(final UUID uuid) {
final int rows = this.jdbcTemplate
.update(this.eventQueriesProperties.getProperty(CrudQueriesOperations.DELETE_ID.name()), uuid);

final boolean deleted = rows == 1 ? true : false;
final boolean deleted = rows == 1;

if (deleted) {

Expand Down Expand Up @@ -181,7 +181,7 @@ private Event editEvent(final EventDto dto) {

final UUID uuid = dto.uuid();

final Timestamp createdAt = Timestamp.from(this.getEvent(uuid).getCreatedAt());
final Timestamp createdAt = this.getCreatedAt(uuid);
final Timestamp timestamp = Timestamp.from(Instant.now());
final String name = dto.denomination();
final String place = dto.place();
Expand Down Expand Up @@ -224,10 +224,9 @@ private UUID[] convertToArray(final List<UUID> ids) {

}

private AbstractDomainObject getEvent(final UUID uuid) {
private Timestamp getCreatedAt(final UUID uuid) {

return this.findById(uuid).get();
return Timestamp.from(this.findById(uuid).map(AbstractDomainObject::getCreatedAt).orElse(Instant.now()));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public boolean deleteById(final UUID uuid) {
final int rows = this.jdbcTemplate
.update(this.organizerQueriesProperties.getProperty(CrudQueriesOperations.DELETE_ID.name()), uuid);

final boolean deleted = rows == 1 ? true : false;
final boolean deleted = rows == 1;

if (deleted) {

Expand Down Expand Up @@ -172,7 +172,7 @@ private Organizer saveOrganizer(final OrganizerDto organizer) {
private Organizer editOrganizer(final OrganizerDto organizer) {

final UUID uuid = organizer.uuid();
final Timestamp createdAt = Timestamp.from(this.getOrganizer(uuid).getCreatedAt());
final Timestamp createdAt = this.getCreatedAt(uuid);
final Timestamp timestamp = Timestamp.from(Instant.now());
final String name = organizer.denomination();
final String website = organizer.website();
Expand Down Expand Up @@ -207,9 +207,9 @@ private String[] convertToArray(final List<EventType> ticketIds) {

}

private AbstractDomainObject getOrganizer(final UUID id) {
private Timestamp getCreatedAt(final UUID uuid) {

return this.findById(id).get();
return Timestamp.from(this.findById(uuid).map(AbstractDomainObject::getCreatedAt).orElse(Instant.now()));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public boolean deleteById(final UUID uuid) {
final int rows = this.jdbcTemplate
.update(this.sponsorQueriesProperties.getProperty(CrudQueriesOperations.DELETE_ID.name()), uuid);

final boolean deleted = rows == 1 ? true : false;
final boolean deleted = rows == 1;

if (deleted) {

Expand Down Expand Up @@ -142,7 +142,7 @@ public Collection<Sponsor> findAll() {
private Sponsor saveSponsor(final SponsorDto sponsor) {

final UUID sponsorUuid = sponsor.uuid();
Instant now = Instant.now();
final Instant now = Instant.now();
final Timestamp createdAt = Timestamp.from(now);
final Timestamp timestamp = Timestamp.from(now);
final String name = sponsor.denomination();
Expand All @@ -164,7 +164,7 @@ private Sponsor saveSponsor(final SponsorDto sponsor) {
private Sponsor editOrganizer(final SponsorDto sponsor) {

final UUID uuid = sponsor.uuid();
final Timestamp createdAt = Timestamp.from(this.getSponsor(uuid).getCreatedAt());
final Timestamp createdAt = this.getCreatedAt(uuid);
final Timestamp timestamp = Timestamp.from(Instant.now());
final String name = sponsor.denomination();
final String website = sponsor.website();
Expand All @@ -180,10 +180,9 @@ private Sponsor editOrganizer(final SponsorDto sponsor) {

}

private AbstractDomainObject getSponsor(final UUID id) {
private Timestamp getCreatedAt(final UUID uuid) {

return this.findById(id).get();
return Timestamp.from(this.findById(uuid).map(AbstractDomainObject::getCreatedAt).orElse(Instant.now()));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public boolean deleteById(final UUID uuid) {
final int rows = this.jdbcTemplate
.update(this.ticketQueriesProperties.getProperty(CrudQueriesOperations.DELETE_ID.name()), uuid);

final boolean deleted = rows == 1 ? true : false;
final boolean deleted = rows == 1;

if (deleted) {

Expand Down Expand Up @@ -166,7 +166,7 @@ private Ticket saveTicket(final TicketDto ticket) {
private Ticket editTicket(final TicketDto ticket) {

final UUID uuid = ticket.uuid();
final Timestamp createdAt = Timestamp.from(this.getTicket(uuid).getCreatedAt());
final Timestamp createdAt = this.getCreatedAt(uuid);
final Timestamp timestamp = Timestamp.from(Instant.now());
final UUID eventId = ticket.eventID();
final TicketType ticketType = ticket.ticketType();
Expand All @@ -183,9 +183,9 @@ private Ticket editTicket(final TicketDto ticket) {

}

private AbstractDomainObject getTicket(final UUID id) {
private Timestamp getCreatedAt(final UUID uuid) {

return this.findById(id).get();
return Timestamp.from(this.findById(uuid).map(AbstractDomainObject::getCreatedAt).orElse(Instant.now()));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@PropertySource("classpath:properties/queries.properties")
public class Queries {

public static enum CrudQueriesOperations {
public enum CrudQueriesOperations {

SAVE, EDIT, GET_ALL, GET_ID, DELETE_ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public Attendee mapRow(final ResultSet rs,
final int rowNum)
throws SQLException {

final List<UUID> ticket_ids = this.arrayToListOfUuid.apply(rs.getArray("ticket_ids"));
final List<UUID> ticketIds = this.arrayToListOfUuid.apply(rs.getArray("ticket_ids"));

return new Attendee(UUID.fromString(rs.getString("id")), rs.getTimestamp("created_at").toInstant(),
rs.getTimestamp("last_updated").toInstant(), rs.getString("first_name"), rs.getString("last_name"),
ticket_ids);
ticketIds);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ private Duration extractDuration(final PGInterval pgInterval) {
final double seconds = pgInterval.getSeconds();
final int ms = pgInterval.getMicroSeconds();

final Duration duration = Duration.ofHours(hours).plusMinutes(mins).plusSeconds((long) seconds).plusMillis(ms);

return duration;
return Duration.ofHours(hours).plusMinutes(mins).plusSeconds((long) seconds).plusMillis(ms);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public Organizer mapRow(final ResultSet rs,
final int rowNum)
throws SQLException {

final List<EventType> listOfEventTypes = this.arrayToListOfEventTypes.apply(rs.getArray("event_types"));
final List<EventType> eventsTypes = this.arrayToListOfEventTypes.apply(rs.getArray("event_types"));

final ContactInformation contactInformation = new ContactInformation(rs.getString("email"),
rs.getString("phone_number"), rs.getString("physical_address"));

return new Organizer(UUID.fromString(rs.getString("id")), rs.getTimestamp("created_at").toInstant(),
rs.getTimestamp("last_updated").toInstant(), rs.getString("denomination"), rs.getString("website"),
rs.getString("information"), listOfEventTypes, contactInformation);
rs.getString("information"), eventsTypes, contactInformation);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Array;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
Expand All @@ -23,11 +24,10 @@ public List<UUID> apply(final Array array) {

try {

for (final UUID uuid : (UUID[]) array.getArray()) {
final UUID[] ss = (UUID[]) array.getArray();

list.add(uuid);
Collections.addAll(list, ss);

}
} catch (final SQLException e) {

LOGGER.error("Exception occured", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import java.util.Optional;
import java.util.UUID;

public interface IService<T, T_DTO> extends ILookUpService<T> {
public interface IService<T, DTO> extends ILookUpService<T> {

T add(T_DTO object);
T add(DTO object);

@Override
Optional<T> get(UUID uuid);

void delete(UUID uuid);

T edit(UUID uuid,
T_DTO object);
DTO object);

Collection<T> getAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,7 @@ public boolean addAttendee(final UUID eventId,

final Event eventFromRepo = this.eventRepository.edit(dto);

if (!eventFromRepo.getAttendeesIDs().containsAll(list)) {

return false;
}

return true;
return eventFromRepo.getAttendeesIDs().containsAll(list);

}

Expand Down

0 comments on commit 728b8d8

Please sign in to comment.