Skip to content

Commit

Permalink
feat: updates and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Campos committed Apr 8, 2022
1 parent bd22dde commit afcda3e
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 243 deletions.
4 changes: 3 additions & 1 deletion src/main/java/fastvagas/controller/GuestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public Contact send(@RequestBody Contact contact) {
@PostMapping(value = "/do-crowler", produces = "application/json")
public ResponseEntity<?> crowlerTests() {
crowlerService.start();
LocalDateTime ultimoMes = DateUtil.getCurrentLocalDateTime().minusDays(31L);
jobService.processUserJobs(ultimoMes);
return ResponseEntity.ok().body("Done");
}

Expand Down Expand Up @@ -128,7 +130,7 @@ public List<PortalJobResponse> getJobs() {
public ResponseEntity<?> reprocessUser(@RequestBody User user) {
try {
LocalDateTime ultimoMes = DateUtil.getCurrentLocalDateTime().minusDays(31L);
jobService.reprocessUserJobs(user.getUser_id(), ultimoMes);
jobService.processUserJobs(user.getUser_id(), ultimoMes);
return ResponseEntity.ok().body("Done");
} catch (Exception e) {
e.printStackTrace();
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/fastvagas/data/dao/CrowlerLogDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ public CrowlerLogDao(NamedParameterJdbcTemplate template) {
super(CrowlerLog.class, template, new CrowlerLogRowMapper());
}

public Integer getLastSequenceByDate(LocalDate pDate) {
List<CrowlerLog> list = findAllByPrimaryKey(pDate, null);

if (list.isEmpty()) {
return 0;
}
public Integer getNextSequence() {
final String query = "SELECT max(" + CrowlerLog.SEQUENCE + ") FROM " + CrowlerLog.TABLE;
return queryForObjectInt(query) + 1;
}

CrowlerLog last = list.get(list.size()-1);
return last.getSequence();
public Integer selectCount() {
final String query = "SELECT count(*) FROM " + CrowlerLog.TABLE;
return queryForObjectInt(query);
}

public List<CrowlerLog> findAllByPrimaryKey(LocalDate created_at, Integer sequence) {
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/fastvagas/data/dao/Dao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fastvagas.data.dao;

import fastvagas.exception.DatabaseException;
import fastvagas.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
Expand All @@ -11,7 +12,10 @@
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import javax.validation.constraints.Size;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -52,6 +56,40 @@ T getObjectFromResult(String query, SqlParameterSource params) {
}
}

Integer queryForObjectInt(String query, SqlParameterSource params) {
try {
log.info(query);

Integer count = template.queryForObject(query, params, Integer.class);
log.info("Count: {}", count);

return count;
} catch (DataAccessException ex) {
throw new DatabaseException(
"Operação não realizada. Contate o administrador do sistema.",
ex,
entityClass.getName() + ": queryForObjectInt DataAccessException: " + ex.getLocalizedMessage()
);
}
}

Integer queryForObjectInt(String query) {
try {
log.info(query);

Integer count = template.queryForObject(query, new HashMap<>(), Integer.class);
log.info("Count: {}", count);

return count;
} catch (DataAccessException ex) {
throw new DatabaseException(
"Operação não realizada. Contate o administrador do sistema.",
ex,
entityClass.getName() + ": queryForObjectInt DataAccessException: " + ex.getLocalizedMessage()
);
}
}

List<T> getListFromResult(String query) {
try {
log.info(query);
Expand Down Expand Up @@ -119,6 +157,8 @@ int executeInsert(String query, Map<String, ?> map) {

int[] executeInsertBatch(String query, List<T> list) {
try {
checkFields(list);

log.info("SQL: " + query);

SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(list.toArray());
Expand Down Expand Up @@ -164,4 +204,38 @@ protected long getGeneratedId(String keyName) {

return id;
}

private void checkFields(List<T> list) {
log.info("Checking values for: " + entityClass.getName());

for (T t : list) {
Field[] fields = t.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);

if (!field.isAnnotationPresent(Size.class)) {
continue;
}

try {
Object value = field.get(t);
if (!ObjectUtil.hasValue(value)) {
continue;
}

int max = field.getAnnotation(Size.class).max();
int len = value.toString().length();

if (len > max) {
log.warn(entityClass.getName() + ": campo " + field.getName() + " tem valor (" + len +
") maior do que o permitido (" + max + ")!");
String newValue = value.toString().substring(0, max);
field.set(t, newValue);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/fastvagas/data/dao/PortalJobDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public List<PortalJob> findAll() {
return getListFromResult("SELECT * FROM " + PortalJob.TABLE);
}

public List<PortalJob> findAllByPortalIdPublishedRange(Long portal_id, Date published_at_start) {
public List<PortalJob> findAllByPortalIdCreatedAtFrom(Long portal_id, LocalDateTime createdAtFrom) {
final String query = "SELECT * "
+ " FROM " + PortalJob.TABLE
+ " WHERE " + PortalJob.PORTAL_ID + "=:" + PortalJob.PORTAL_ID
+ " AND " + PortalJob.PUBLISHED_AT + " > :" + PortalJob.PUBLISHED_AT
+ " AND " + PortalJob.CREATED_AT + " >= :" + PortalJob.CREATED_AT
+ " ORDER BY " + PortalJob.PUBLISHED_AT + " DESC"
+ " , " + PortalJob.NAME + " ASC";

SqlParameterSource params = new MapSqlParameterSource()
.addValue(PortalJob.PORTAL_ID, portal_id)
.addValue(PortalJob.PUBLISHED_AT, published_at_start);
.addValue(PortalJob.CREATED_AT, createdAtFrom);

return getListFromResult(query, params);
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/fastvagas/data/dao/UserTermDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fastvagas.data.dao;

import fastvagas.data.entity.User;
import fastvagas.data.entity.UserTerm;
import fastvagas.data.mapper.UserTermRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
Expand Down Expand Up @@ -43,6 +44,17 @@ public List<UserTerm> findAllByUserId(Long user_id) {
return getListFromResult(query, params);
}

public List<UserTerm> findAllEnabledUsersTerms() {
final String query = "SELECT " + UserTerm.TABLE + ".*"
+ " FROM " + UserTerm.TABLE
+ " JOIN " + User.TABLE + " ON ("
+ User.TABLE + "." + User.USER_ID + "=" + UserTerm.TABLE + "." + UserTerm.USER_ID
+ ")"
+ " WHERE " + User.ENABLED + "= 1";

return getListFromResult(query);
}

public UserTerm create(UserTerm userTerm) {
final String query = "INSERT INTO " + UserTerm.TABLE + " ("
+ UserTerm.TERMS
Expand Down
60 changes: 12 additions & 48 deletions src/main/java/fastvagas/data/entity/CrowlerLog.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package fastvagas.data.entity;

import fastvagas.util.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.Objects;

@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class CrowlerLog {

public static final String TABLE = "crowler_log";
Expand All @@ -18,53 +29,6 @@ public class CrowlerLog {
private Long portal_id;
private String text;


public LocalDate getCreated_at() {
return created_at;
}

public void setCreated_at(LocalDate created_at) {
this.created_at = created_at;
}

public Integer getSequence() {
return sequence;
}

public void setSequence(Integer sequence) {
this.sequence = sequence;
}

public Long getPortal_id() {
return portal_id;
}

public void setPortal_id(Long portal_id) {
this.portal_id = portal_id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CrowlerLog that = (CrowlerLog) o;
return created_at.equals(that.created_at) &&
sequence.equals(that.sequence);
}

@Override
public int hashCode() {
return Objects.hash(created_at, sequence);
}

@Override
public String toString() {
return "crowler_log={"
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/fastvagas/data/entity/PortalJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

@Getter
@Setter
Expand All @@ -26,12 +25,25 @@ public class PortalJob {
public static final String CREATED_AT = "created_at";

private Long portal_job_id;

@Size(max=600)
private String name;

@Size(max=600)
private String company_name;

@Size(max=30)
private String job_type;

@Size(max=600)
private String description;

@Size(max=30)
private String published_at;

@Size(max=1000)
private String url;

private Long portal_id;
private Long city_id;
private LocalDateTime created_at;
Expand Down
Loading

0 comments on commit afcda3e

Please sign in to comment.