Added saving conifg to databse

This commit is contained in:
Filip Izydorczyk 2020-12-09 16:43:29 +01:00
parent f08e7cf1e5
commit 0cee454356
5 changed files with 147 additions and 2 deletions

View File

@ -0,0 +1,94 @@
package com.plannaplan.entities;
import java.sql.Date;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.plannaplan.models.TourData;
/**
* entity that keeps app configurations
*/
@Entity
public class AppConfig {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Date firstTourStart;
private Date firstTourEnd;
private Date secondTourStart;
private Date secondTourEnd;
private Timestamp configDate;
/**
* no parameter construcotor
*/
public AppConfig() {
}
/**
* constructor
*
* @param firstTour first TourData instacne
* @param scondTour second TourData instacne
*/
public AppConfig(TourData firstTour, TourData scondTour) {
this.firstTourStart = firstTour.getStart();
this.firstTourEnd = firstTour.getEnd();
this.secondTourStart = scondTour.getStart();
this.secondTourEnd = scondTour.getEnd();
this.configDate = new Timestamp(System.currentTimeMillis());
}
/**
*
* second tour end getter
*
* @return Date inforamtion when second tour ends
*/
public Date getSecondTourEnd() {
return secondTourEnd;
}
/**
* first second start getter
*
* @return Date inforamtion when second tour start
*/
public Date getSecondTourStart() {
return secondTourStart;
}
/**
* first tour end getter
*
* @return Date inforamtion when first tour end
*/
public Date getFirstTourEnd() {
return firstTourEnd;
}
/**
* first tour start getter
*
* @return Date inforamtion when first tour start
*/
public Date getFirstTourStart() {
return firstTourStart;
}
/**
* config date getter
*
* @return Timestamp when configuration took place
*/
public Timestamp getConfigDate() {
return configDate;
}
}

View File

@ -0,0 +1,9 @@
package com.plannaplan.repositories;
import com.plannaplan.entities.AppConfig;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AppConfigRepository extends JpaRepository<AppConfig, Long> {
}

View File

@ -0,0 +1,26 @@
package com.plannaplan.services;
import com.plannaplan.entities.AppConfig;
import com.plannaplan.repositories.AppConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AppConfigService {
@Autowired
private AppConfigRepository repo;
public AppConfigService() {
}
/**
* saves config instance to dadabase
*
* @param appConfig AppConfig instance with correct dates
* @return AppConfig with id from databse after save
*/
public AppConfig save(AppConfig appConfig) {
return this.repo.save(appConfig);
}
}

View File

@ -7,22 +7,31 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.plannaplan.configutils.*;
import com.plannaplan.entities.AppConfig;
/**
* FileReader is used for reading xls file from input stream.
* FileReader is used for reading xls file from input stream.
*/
@Component
public class ConfiguratorService {
@Autowired
private FileToDatabaseMigrator migrator;
@Autowired
private AppConfigService configService;
public ConfiguratorService() {
}
/**
* methoid to config system
*
* @param data ConfigData containng system congis
*/
public void config(ConfigData data) {
FileReader reader = new FileReader(data.getFilestream());
FileData coursesData = reader.read();
this.configService.save(new AppConfig(data.getFirstTour(), data.getSecondTour()));
migrator.migrate(coursesData);
}

View File

@ -1,8 +1,10 @@
package com.plannaplan;
import java.io.InputStream;
import java.sql.Date;
import com.plannaplan.models.ConfigData;
import com.plannaplan.models.TourData;
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
@ -44,7 +46,12 @@ public class App {
if (this.isDev) {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx");
ConfigData data = new ConfigData(null, null, inputStream);
ConfigData data = new ConfigData(
new TourData(new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis())),
new TourData(new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis())),
inputStream);
this.contrl.config(data);
User newuser = new User();