Creating entities with corresponding fields with corresponding constraints about relations, dao's and database creation file
(singletton pattern)
This commit is contained in:
parent
66f3ab66f6
commit
7dc95277c2
@ -9,6 +9,7 @@ import java.util.List;
|
|||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.Repository.MyRealEstateAgencyRepository;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.AgencyWithAgents;
|
import fr.romanet.vj.apps.myrealestateagency.entities.AgencyWithAgents;
|
||||||
@ -25,37 +26,23 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void insertSingleTodo(View view) {
|
public void insertSingleTodo(View view) {
|
||||||
new Thread(new Runnable() {
|
Agency test1 = new Agency("AgenceOFParis");
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Log.d("MyApp","UNNNNNN GROOOOOOOOOOOOS CAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAA");
|
|
||||||
Agency test1 = new Agency("AgenceOFParis");
|
|
||||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
|
||||||
.agencyDao().insertAgency(test1);
|
|
||||||
|
|
||||||
Agent test = new Agent("Bader", "Guetari", 1);
|
Agency test2 = new Agency("AgenceOFMarseille");
|
||||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
|
||||||
.agentDao()
|
|
||||||
.insertAgent(test);
|
|
||||||
|
|
||||||
Agency test2 = new Agency("AgenceOFMarseille");
|
Agent test = new Agent("Bader", "Guetari", 1);
|
||||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
|
||||||
.agencyDao().insertAgency(test2);
|
|
||||||
|
|
||||||
Agent test3 = new Agent("Khalil", "Guetari", 2);
|
Agent test3 = new Agent("Khalil", "Guetari", 2);
|
||||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
|
||||||
.agentDao()
|
|
||||||
.insertAgent(test3);
|
|
||||||
|
|
||||||
Agent test10 = new Agent("Achref", "Guetari", 1);
|
Agent test10 = new Agent("Achref", "Guetari", 1);
|
||||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
|
||||||
.agentDao()
|
|
||||||
.insertAgent(test10);
|
|
||||||
|
|
||||||
String query = "SELECT agency_id FROM agency INNER JOIN agent ON agency.agency_id = agent.agent_id WHERE agency_name =" + "AgenceOFParis" + "GROUP BY agency_name";
|
MyRealEstateAgencyRepository.getInstance(this).addAgency(test1);
|
||||||
String query1 = "SELECT agency_id FROM agent INNER JOIN agency ON agency.agency_id = agent.agent_id WHERE agency_name =" + "AgenceOFParis" + "GROUP BY agency_name";
|
MyRealEstateAgencyRepository.getInstance(this).addAgency(test2);
|
||||||
|
MyRealEstateAgencyRepository.getInstance(this).addAgent(test);
|
||||||
|
MyRealEstateAgencyRepository.getInstance(this).addAgent(test3);
|
||||||
|
MyRealEstateAgencyRepository.getInstance(this).addAgent(test10);
|
||||||
|
|
||||||
}
|
String query = "SELECT agency_id FROM agency INNER JOIN agent ON agency.agency_id = agent.agent_id WHERE agency_name =" + "AgenceOFParis" + "GROUP BY agency_name";
|
||||||
}).start();
|
String query1 = "SELECT agency_id FROM agent INNER JOIN agency ON agency.agency_id = agent.agent_id WHERE agency_name =" + "AgenceOFParis" + "GROUP BY agency_name";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
package fr.romanet.vj.apps.myrealestateagency.Repository;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import androidx.room.Room;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
||||||
|
|
||||||
|
public final class MyRealEstateAgencyRepository {
|
||||||
|
private static volatile MyRealEstateAgencyRepository instance;
|
||||||
|
|
||||||
|
public static MyRealEstateAgencyRepository getInstance(Context context)
|
||||||
|
{
|
||||||
|
if (instance == null)
|
||||||
|
{
|
||||||
|
synchronized (MyRealEstateAgencyRepository.class)
|
||||||
|
{
|
||||||
|
if (instance == null)
|
||||||
|
{
|
||||||
|
instance = new MyRealEstateAgencyRepository(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final RealEstateAgencyDatabase realEstateAgencyDatabaseDatabase;
|
||||||
|
|
||||||
|
private MyRealEstateAgencyRepository(Context context)
|
||||||
|
{
|
||||||
|
realEstateAgencyDatabaseDatabase = Room.databaseBuilder(context, RealEstateAgencyDatabase.class, "real_estate_agency_db").allowMainThreadQueries().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAgency(Agency agency){realEstateAgencyDatabaseDatabase.agencyDao().insertAgency(agency);}
|
||||||
|
public void addAgent(Agent agent){realEstateAgencyDatabaseDatabase.agentDao().insertAgent(agent);}
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package fr.romanet.vj.apps.myrealestateagency.dao;
|
||||||
|
|
||||||
|
import androidx.room.Dao;
|
||||||
|
import androidx.room.Query;
|
||||||
|
import androidx.room.Transaction;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.entities.AgencyWithProperties;
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
public interface AgencyWithPropertiesDao {
|
||||||
|
@Transaction
|
||||||
|
@Query("SELECT * FROM agency")
|
||||||
|
List<AgencyWithProperties> getAgenciesAndProperties();
|
||||||
|
}
|
||||||
@ -1,19 +0,0 @@
|
|||||||
package fr.romanet.vj.apps.myrealestateagency.dao;
|
|
||||||
|
|
||||||
import androidx.room.Dao;
|
|
||||||
import androidx.room.Insert;
|
|
||||||
import androidx.room.Query;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.PropertyStatue;
|
|
||||||
|
|
||||||
|
|
||||||
@Dao
|
|
||||||
public interface PropertyStatueDao {
|
|
||||||
@Query("SELECT * FROM property_statue")
|
|
||||||
List<PropertyStatue> getPropertyStatueList();
|
|
||||||
|
|
||||||
@Insert
|
|
||||||
void insertPropertyStatue(PropertyStatue propertyStatue);
|
|
||||||
}
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
package fr.romanet.vj.apps.myrealestateagency.dao;
|
|
||||||
|
|
||||||
import androidx.room.Dao;
|
|
||||||
import androidx.room.Insert;
|
|
||||||
import androidx.room.Query;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.PropertyType;
|
|
||||||
|
|
||||||
@Dao
|
|
||||||
public interface PropertyTypeDao {
|
|
||||||
@Query("SELECT * FROM property_type")
|
|
||||||
List<PropertyType> getPropertyTypeList();
|
|
||||||
|
|
||||||
@Insert
|
|
||||||
void insertPropertyType(PropertyType propertyType);
|
|
||||||
}
|
|
||||||
@ -9,40 +9,21 @@ import androidx.room.TypeConverters;
|
|||||||
|
|
||||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyDao;
|
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyDao;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyWithAgentsDao;
|
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyWithAgentsDao;
|
||||||
|
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyWithPropertiesDao;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyDao;
|
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyDao;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyStatueDao;
|
|
||||||
import fr.romanet.vj.apps.myrealestateagency.dao.PropertyTypeDao;
|
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgentDao;
|
import fr.romanet.vj.apps.myrealestateagency.dao.AgentDao;
|
||||||
import fr.romanet.vj.apps.myrealestateagency.entities.PropertyStatue;
|
import fr.romanet.vj.apps.myrealestateagency.entities.Property;
|
||||||
|
|
||||||
@Database(entities = {Agency.class, Agent.class}, version = 1)
|
@Database(entities = {Agency.class, Agent.class, Property.class}, version = 1)
|
||||||
@TypeConverters({PropertyStatue.Converters.class})
|
@TypeConverters({Property.Converters.class})
|
||||||
public abstract class RealEstateAgencyDatabase extends RoomDatabase {
|
public abstract class RealEstateAgencyDatabase extends RoomDatabase {
|
||||||
|
|
||||||
public abstract AgentDao agentDao();
|
public abstract AgentDao agentDao();
|
||||||
public abstract AgencyDao agencyDao();
|
public abstract AgencyDao agencyDao();
|
||||||
public abstract AgencyWithAgentsDao agencyWithAgentsDao();
|
|
||||||
public abstract PropertyStatueDao statueDao();
|
|
||||||
public abstract PropertyTypeDao propertyTypeDao();
|
|
||||||
public abstract PropertyDao propertyDao();
|
public abstract PropertyDao propertyDao();
|
||||||
private static volatile RealEstateAgencyDatabase INSTANCE;
|
public abstract AgencyWithAgentsDao agencyWithAgentsDao();
|
||||||
|
public abstract AgencyWithPropertiesDao agencyWithPropertiesDaoDao();
|
||||||
public static RealEstateAgencyDatabase getInstance(Context context) {
|
|
||||||
if(INSTANCE == null){
|
|
||||||
synchronized (RealEstateAgencyDatabase.class) {
|
|
||||||
if(INSTANCE == null) {
|
|
||||||
INSTANCE = Room
|
|
||||||
.databaseBuilder(context.getApplicationContext()
|
|
||||||
, RealEstateAgencyDatabase.class, "real_estate_agency_db")
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,15 @@
|
|||||||
|
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||||
|
|
||||||
|
import androidx.room.Embedded;
|
||||||
|
import androidx.room.Relation;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AgencyWithProperties {
|
||||||
|
public int agency_id;
|
||||||
|
@Relation(
|
||||||
|
parentColumn = "agency_id",
|
||||||
|
entityColumn = "belongs_agency_id"
|
||||||
|
)
|
||||||
|
public List<AgencyWithProperties> agents;
|
||||||
|
}
|
||||||
@ -1,11 +1,23 @@
|
|||||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||||
|
|
||||||
import androidx.room.ColumnInfo;
|
import androidx.room.ColumnInfo;
|
||||||
|
import androidx.room.Embedded;
|
||||||
import androidx.room.Entity;
|
import androidx.room.Entity;
|
||||||
|
import androidx.room.ForeignKey;
|
||||||
import androidx.room.Index;
|
import androidx.room.Index;
|
||||||
import androidx.room.PrimaryKey;
|
import androidx.room.PrimaryKey;
|
||||||
|
import androidx.room.TypeConverter;
|
||||||
|
|
||||||
@Entity(tableName = "property", indices = {@Index(value = {"address","longitude", "latitude"}, unique = true)})
|
import java.sql.Date;
|
||||||
|
|
||||||
|
import static androidx.room.ForeignKey.CASCADE;
|
||||||
|
|
||||||
|
@Entity(tableName = "property",
|
||||||
|
indices = {@Index(value = "property_id") ,@Index(value = {"address","longitude", "latitude"}, unique = true)},
|
||||||
|
foreignKeys = @ForeignKey(entity = Agency.class,
|
||||||
|
parentColumns = "agency_id",
|
||||||
|
childColumns = "belongs_agency_id",
|
||||||
|
onDelete = CASCADE))
|
||||||
public class Property {
|
public class Property {
|
||||||
|
|
||||||
@PrimaryKey(autoGenerate = true)
|
@PrimaryKey(autoGenerate = true)
|
||||||
@ -19,12 +31,18 @@ public class Property {
|
|||||||
public String address;
|
public String address;
|
||||||
|
|
||||||
@ColumnInfo(name = "longitude")
|
@ColumnInfo(name = "longitude")
|
||||||
public double longitude;
|
public long longitude;
|
||||||
|
|
||||||
@ColumnInfo(name = "latitude")
|
@ColumnInfo(name = "latitude")
|
||||||
public double latitude;
|
public long latitude;
|
||||||
|
|
||||||
public Property(String description, String address, double longitude, double latitude)
|
//@Embedded
|
||||||
|
//public PropertyStatue propertyStatue;
|
||||||
|
|
||||||
|
@ColumnInfo(name = "belongs_agency_id")
|
||||||
|
public int belongsToAgencyId;
|
||||||
|
|
||||||
|
public Property(String description, String address, long longitude, long latitude)
|
||||||
{
|
{
|
||||||
propertyId = 0;
|
propertyId = 0;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
@ -32,4 +50,17 @@ public class Property {
|
|||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
this.latitude = latitude;
|
this.latitude = latitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Converters {
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
public static Date toDate(Long dateLong){
|
||||||
|
return dateLong == null ? null: new Date(dateLong);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TypeConverter
|
||||||
|
public static Long fromDate(Date date){
|
||||||
|
return date == null ? null : date.getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,24 +2,14 @@ package fr.romanet.vj.apps.myrealestateagency.entities;
|
|||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.room.ColumnInfo;
|
import androidx.room.ColumnInfo;
|
||||||
import androidx.room.Entity;
|
import androidx.room.Embedded;
|
||||||
import androidx.room.ForeignKey;
|
|
||||||
import androidx.room.PrimaryKey;
|
|
||||||
import androidx.room.TypeConverter;
|
|
||||||
|
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
|
|
||||||
import static androidx.room.ForeignKey.CASCADE;
|
|
||||||
|
|
||||||
@Entity(tableName = "property_statue", foreignKeys = @ForeignKey(entity = Property.class, parentColumns = "property_id", childColumns = "belongs_property_id", onDelete = CASCADE))
|
|
||||||
public class PropertyStatue{
|
public class PropertyStatue{
|
||||||
|
|
||||||
@ColumnInfo(name = "statue_id")
|
@ColumnInfo(name = "statue_sale")
|
||||||
@PrimaryKey(autoGenerate = true)
|
public boolean statueSale;
|
||||||
public int statueId;
|
|
||||||
|
|
||||||
@ColumnInfo(name = "statue_description")
|
|
||||||
public boolean statueDescription;
|
|
||||||
|
|
||||||
@ColumnInfo(name = "sold_date")
|
@ColumnInfo(name = "sold_date")
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -28,27 +18,7 @@ public class PropertyStatue{
|
|||||||
@ColumnInfo(name = "price")
|
@ColumnInfo(name = "price")
|
||||||
public double price;
|
public double price;
|
||||||
|
|
||||||
@ColumnInfo(name = "belongs_property_id")
|
@Embedded
|
||||||
public int propertyId;
|
public PropertyType propertyType;
|
||||||
|
|
||||||
public PropertyStatue(boolean statueDescription, double price, int propertyId)
|
|
||||||
{
|
|
||||||
statueId = 0;
|
|
||||||
this.statueDescription = statueDescription;
|
|
||||||
this.price = price;
|
|
||||||
this.propertyId = propertyId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Converters {
|
|
||||||
|
|
||||||
@TypeConverter
|
|
||||||
public static Date toDate(Long dateLong){
|
|
||||||
return dateLong == null ? null: new Date(dateLong);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TypeConverter
|
|
||||||
public static Long fromDate(Date date){
|
|
||||||
return date == null ? null : date.getTime();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,19 +1,9 @@
|
|||||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||||
|
|
||||||
import androidx.room.ColumnInfo;
|
import androidx.room.ColumnInfo;
|
||||||
import androidx.room.Entity;
|
|
||||||
import androidx.room.ForeignKey;
|
|
||||||
import androidx.room.PrimaryKey;
|
|
||||||
|
|
||||||
import static androidx.room.ForeignKey.CASCADE;
|
|
||||||
|
|
||||||
@Entity(tableName = "property_type", foreignKeys = @ForeignKey(entity = Property.class, parentColumns = "property_id", childColumns = "belongs_property_id", onDelete = CASCADE))
|
|
||||||
public class PropertyType {
|
public class PropertyType {
|
||||||
|
|
||||||
@PrimaryKey(autoGenerate = true)
|
|
||||||
@ColumnInfo(name = "type_id")
|
|
||||||
public int typeId;
|
|
||||||
|
|
||||||
@ColumnInfo(name = "type_description")
|
@ColumnInfo(name = "type_description")
|
||||||
public String typeDescription;
|
public String typeDescription;
|
||||||
|
|
||||||
@ -23,15 +13,4 @@ public class PropertyType {
|
|||||||
@ColumnInfo(name = "surface_area")
|
@ColumnInfo(name = "surface_area")
|
||||||
public float surfaceArea;
|
public float surfaceArea;
|
||||||
|
|
||||||
@ColumnInfo(name = "belongs_property_id")
|
|
||||||
public int propertyId;
|
|
||||||
|
|
||||||
public PropertyType(String typeDescription, int numberRooms, float surfaceArea, int propertyId)
|
|
||||||
{
|
|
||||||
typeId = 0;
|
|
||||||
this.typeDescription = typeDescription;
|
|
||||||
this.numberRooms = numberRooms;
|
|
||||||
this.surfaceArea = surfaceArea;
|
|
||||||
this.propertyId = propertyId;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user