diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..fdc392f --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index cf95fe9..56d4a95 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/backend.iml b/backend.iml new file mode 100644 index 0000000..1eae0df --- /dev/null +++ b/backend.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index da2744c..c65502c 100644 --- a/build.gradle +++ b/build.gradle @@ -13,8 +13,14 @@ repositories { } dependencies { + compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final' implementation 'org.springframework.boot:spring-boot-starter' - implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.postgresql:postgresql' + implementation('org.springframework.boot:spring-boot-starter-web') { + exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json' + } + implementation 'com.google.code.gson:gson' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9492014..52d63d1 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Thu Feb 20 21:04:58 CET 2020 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java deleted file mode 100644 index e1fda82..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/HelloWorld.java +++ /dev/null @@ -1,15 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut; - -import org.springframework.boot.*; -import org.springframework.boot.autoconfigure.*; -import org.springframework.web.bind.annotation.*; - -@RestController -@EnableAutoConfiguration -public class HelloWorld { - - @RequestMapping("/") - String home() { - return "Hello World!"; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java index ba489c5..f215fe7 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java @@ -5,7 +5,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SmarthutApplication { - public static void main(String[] args) { SpringApplication.run(SmarthutApplication.class, args); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java new file mode 100644 index 0000000..e462306 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java @@ -0,0 +1,24 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.util.*; +import org.springframework.boot.autoconfigure.*; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +@RestController +@EnableAutoConfiguration +@Controller +public class WelcomeController { + + @GetMapping("/") + List testDevices() { + return Arrays.asList( + new KnobDimmer(), + new RegularLight(), + new MotionSensor(), + new Sensor(), + new SmartPlug(), + new Switch()); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java new file mode 100644 index 0000000..e666c3e --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java @@ -0,0 +1,14 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; + +/** + * Represents a dimmer that can only instruct an increase or decrease of intensity (i.e. like a + * dimmer with a '+' and a '-' button) + */ +@Entity +public class ButtonDimmer extends Dimmer { + public ButtonDimmer() { + super("button-dimmer"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmerRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmerRepository.java new file mode 100644 index 0000000..a74e68f --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmerRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface ButtonDimmerRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java new file mode 100644 index 0000000..078a7ad --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -0,0 +1,71 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.*; + +/** Generic abstraction for a smart home device */ +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +public abstract class Device { + + /** Ways a device can behave in the automation flow. For now only input/output */ + public enum FlowType { + INPUT, + OUTPUT + } + + /** Device identifier */ + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id", updatable = false, nullable = false) + private long id; + + /** The room this device belongs in */ + @ManyToOne + @JoinColumn(name = "room_id") + private Room room; + + /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ + @Column private String name; + + /** + * The name for the category of this particular device (e.g 'dimmer'). Not stored in the + * database (thanks to 'transient') but set thanks to constructors + */ + private final transient String kind; + + /** + * The way this device behaves in the automation flow. Not stored in the database (thanks to + * 'transient') but set thanks to constructors + */ + private final transient FlowType flowType; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Room getRoom() { + return room; + } + + public void setRoom(Room room) { + this.room = room; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Device(String kind, FlowType flowType) { + this.kind = kind; + this.flowType = flowType; + this.name = kind + " " + this.id; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java new file mode 100644 index 0000000..666fbee --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java @@ -0,0 +1,27 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Column; +import javax.persistence.Entity; + +/** Represent a dimmable light */ +@Entity +public class DimmableLight extends Light { + + public DimmableLight() { + super("light"); + } + + /** The light intensity value. Goes from 0 (off) to 100 (on) */ + @Column private int intensity = 0; + + public int getIntensity() { + return intensity; + } + + public void setIntensity(int intensity) throws IllegalArgumentException { + if (intensity < 0 || intensity > 100) { + throw new IllegalArgumentException("The intensity level can't go below 0 or above 100"); + } + this.intensity = intensity; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java new file mode 100644 index 0000000..fd3fe43 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface DimmableLightRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java new file mode 100644 index 0000000..2658b35 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -0,0 +1,14 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +/** Represents a generic dimmer input device */ +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +public abstract class Dimmer extends InputDevice { + public Dimmer(String kind) { + super(kind); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java new file mode 100644 index 0000000..e632178 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java @@ -0,0 +1,14 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; + +/** + * A generic abstraction for an input device, i.e. something that captures input either from the + * environment (sensor) or the user (switch / dimmer). + */ +@Entity +public abstract class InputDevice extends Device { + public InputDevice(String kind) { + super(kind, FlowType.INPUT); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java new file mode 100644 index 0000000..80d59ab --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java @@ -0,0 +1,14 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; + +/** + * Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity + * value, like a knob) + */ +@Entity +public class KnobDimmer extends Dimmer { + public KnobDimmer() { + super("knob-dimmer"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmerRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmerRepository.java new file mode 100644 index 0000000..803ed90 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmerRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface KnobDimmerRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Light.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Light.java new file mode 100644 index 0000000..bf1ed39 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Light.java @@ -0,0 +1,29 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +/** Represents a generic light */ +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +public abstract class Light extends OutputDevice { + + /** Whether the light is on or not */ + @Column(name = "light_on") + boolean on; + + protected Light(String kind) { + super(kind); + this.on = false; + } + + public boolean isOn() { + return on; + } + + public void setOn(boolean on) { + this.on = on; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java new file mode 100644 index 0000000..d1c47b3 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java @@ -0,0 +1,11 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; + +/** Represents a motion sensor device */ +@Entity +public class MotionSensor extends InputDevice { + public MotionSensor() { + super("motion-sensor"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensorRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensorRepository.java new file mode 100644 index 0000000..e354262 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensorRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface MotionSensorRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java new file mode 100644 index 0000000..39e5dd0 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java @@ -0,0 +1,17 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +/** + * Represents a generic output device, i.e. something that causes some behaviour (light, smartPlugs, + * ...). + */ +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +public abstract class OutputDevice extends Device { + public OutputDevice(String kind) { + super(kind, FlowType.OUTPUT); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java new file mode 100644 index 0000000..8f07377 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java @@ -0,0 +1,11 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; + +/** Represents a standard non-dimmable light */ +@Entity +public class RegularLight extends Light { + public RegularLight() { + super("regular-light"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLightRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLightRepository.java new file mode 100644 index 0000000..7f3f4a6 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLightRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface RegularLightRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java new file mode 100644 index 0000000..ec732b1 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java @@ -0,0 +1,69 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.Set; +import javax.persistence.*; + +/** Represents a room in the house owned by the user */ +@Entity +public class Room { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id", updatable = false, nullable = false) + private Long id; + + /** User that owns the house this room is in */ + @ManyToOne + @JoinColumn(name = "user_id") + private User user; + + /** The user given name of this room (e.g. 'Master bedroom') */ + @Column private String name; + + /** Collectiion of devices present in this room */ + @OneToMany(mappedBy = "room") + private Set devices; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getDevices() { + return devices; + } + + @Override + public String toString() { + return "Room{" + + "id=" + + id + + ", user=" + + user + + ", name='" + + name + + '\'' + + ", devices=" + + devices + + '}'; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RoomRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RoomRepository.java new file mode 100644 index 0000000..d9efb14 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RoomRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface RoomRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java new file mode 100644 index 0000000..e5d7938 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java @@ -0,0 +1,32 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; + +/** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */ +@Entity +public class Sensor extends InputDevice { + + /** Type of sensor, i.e. of the thing the sensor measures. */ + enum SensorType { + /** A sensor that measures temperature in degrees celsius */ + TEMPERATURE, + + /** A sensor that measures relative humidity in percentage points */ + HUMIDITY, + + /** A sensor that measures light in degrees */ + LIGHT + } + + /** The type of this sensor */ + @Column + @Enumerated(value = EnumType.STRING) + private SensorType sensor; + + public Sensor() { + super("sensor"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SensorRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SensorRepository.java new file mode 100644 index 0000000..cc4eefa --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SensorRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface SensorRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java new file mode 100644 index 0000000..e7fd36a --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java @@ -0,0 +1,25 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Column; +import javax.persistence.Entity; + +/** A smart plug that can be turned either on or off */ +@Entity +public class SmartPlug extends OutputDevice { + + /** Whether the smart plug is on */ + @Column(name = "smart_plug_on") + private boolean on; + + public boolean isOn() { + return on; + } + + public void setOn(boolean on) { + this.on = on; + } + + public SmartPlug() { + super("smart-plug"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlugRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlugRepository.java new file mode 100644 index 0000000..24897a0 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlugRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface SmartPlugRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java new file mode 100644 index 0000000..fbf1eed --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -0,0 +1,11 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; + +/** A switch input device TODO: define switch behaviour (push button vs on/off state) */ +@Entity +public class Switch extends InputDevice { + public Switch() { + super("switch"); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchRepository.java new file mode 100644 index 0000000..c2e0be9 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface SwitchRepository extends CrudRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java new file mode 100644 index 0000000..92202e2 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -0,0 +1,82 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.Set; +import javax.persistence.*; + +/** A user of the Smarthut application */ +@Entity(name = "smarthutuser") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id", updatable = false, nullable = false) + private Long id; + + /** The full name of the user */ + @Column private String name; + + /** A properly salted way to store the password TODO: define the implementation of salt */ + @Column private String hashedPassword; + + /** The user's email TODO: validate email in setters */ + @Column private String email; + + /** All rooms in the user's house */ + @OneToMany(mappedBy = "user") + private Set rooms; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getHashedPassword() { + return hashedPassword; + } + + public void setHashedPassword(String hashedPassword) { + this.hashedPassword = hashedPassword; + } + + public Set getRooms() { + return rooms; + } + + @Override + public String toString() { + return "User{" + + "id=" + + id + + ", name='" + + name + + '\'' + + ", hashedPassword='" + + hashedPassword + + '\'' + + ", email='" + + email + + '\'' + + ", rooms=" + + rooms + + '}'; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java new file mode 100644 index 0000000..a0a0f1f --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface UserRepository extends CrudRepository {} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..7cadd1e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,12 @@ +spring.http.converters.preferred-json-mapper=gson +spring.datasource.url=jdbc:postgresql://localhost:5432/smarthut +spring.datasource.username=postgres +spring.datasource.password= +# Hibernate properties +spring.jpa.database=POSTGRESQL +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl +spring.jpa.properties.hibernate.format_sql=true +, \ No newline at end of file