Creating entities with corresponding fields with corresponding constraints about relations, dao's and database creation file
(singletton pattern)
This commit is contained in:
parent
686ae4e724
commit
66f3ab66f6
@ -2,13 +2,13 @@ package fr.romanet.vj.apps.myrealestateagency;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
|
||||
@ -1,14 +1,61 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.database.RealEstateAgencyDatabase;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agency;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.AgencyWithAgents;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.Agent;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final String TAG = "MyActivity";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
}
|
||||
|
||||
public void insertSingleTodo(View view) {
|
||||
new Thread(new Runnable() {
|
||||
@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);
|
||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
||||
.agentDao()
|
||||
.insertAgent(test);
|
||||
|
||||
Agency test2 = new Agency("AgenceOFMarseille");
|
||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
||||
.agencyDao().insertAgency(test2);
|
||||
|
||||
Agent test3 = new Agent("Khalil", "Guetari", 2);
|
||||
RealEstateAgencyDatabase.getInstance(getApplicationContext())
|
||||
.agentDao()
|
||||
.insertAgent(test3);
|
||||
|
||||
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";
|
||||
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";
|
||||
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
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.Agency;
|
||||
|
||||
@Dao
|
||||
public interface AgencyDao {
|
||||
|
||||
@Query("SELECT * FROM agency")
|
||||
List<Agency> getAgencyList();
|
||||
|
||||
@Insert
|
||||
void insertAgency(Agency agency);
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
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.AgencyWithAgents;
|
||||
|
||||
@Dao
|
||||
public interface AgencyWithAgentsDao {
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM agency")
|
||||
List<AgencyWithAgents> getAgenciesAndAgent();
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
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.Agent;
|
||||
|
||||
@Dao
|
||||
public interface AgentDao {
|
||||
|
||||
@Query("SELECT * FROM agent")
|
||||
List<Agent> getAgentList();
|
||||
|
||||
@Insert
|
||||
void insertAgent(Agent agent);
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
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.Property;
|
||||
|
||||
@Dao
|
||||
public interface PropertyDao {
|
||||
@Query("SELECT * FROM property")
|
||||
List<Property> getPropertyList();
|
||||
|
||||
@Insert
|
||||
void insertProperty(Property property);
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
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);
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
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);
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.database;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.room.Database;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
import androidx.room.TypeConverters;
|
||||
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyDao;
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgencyWithAgentsDao;
|
||||
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.Agent;
|
||||
import fr.romanet.vj.apps.myrealestateagency.dao.AgentDao;
|
||||
import fr.romanet.vj.apps.myrealestateagency.entities.PropertyStatue;
|
||||
|
||||
@Database(entities = {Agency.class, Agent.class}, version = 1)
|
||||
@TypeConverters({PropertyStatue.Converters.class})
|
||||
public abstract class RealEstateAgencyDatabase extends RoomDatabase {
|
||||
|
||||
public abstract AgentDao agentDao();
|
||||
public abstract AgencyDao agencyDao();
|
||||
public abstract AgencyWithAgentsDao agencyWithAgentsDao();
|
||||
public abstract PropertyStatueDao statueDao();
|
||||
public abstract PropertyTypeDao propertyTypeDao();
|
||||
public abstract PropertyDao propertyDao();
|
||||
private static volatile RealEstateAgencyDatabase INSTANCE;
|
||||
|
||||
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,30 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.Index;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
//Parent class of agent (indeed, zero or many agents can work in an agency)
|
||||
@Entity(tableName = "agency",
|
||||
indices = {@Index("agency_id"),
|
||||
@Index(value = {"agency_name"}, unique = true)})
|
||||
public class Agency {
|
||||
|
||||
@ColumnInfo(name = "agency_id")
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
public int agencyId;
|
||||
|
||||
@ColumnInfo(name = "agency_name")
|
||||
public String agencyName;
|
||||
|
||||
public Agency(String agencyName)
|
||||
{
|
||||
agencyId = 0;
|
||||
this.agencyName = agencyName;
|
||||
}
|
||||
|
||||
public int getAgencyId() {
|
||||
return agencyId;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||
|
||||
import androidx.room.Embedded;
|
||||
import androidx.room.Relation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AgencyWithAgents {
|
||||
@Embedded public Agency agency;
|
||||
@Relation(
|
||||
parentColumn = "agency_id",
|
||||
entityColumn = "agency_employer_id"
|
||||
)
|
||||
public List<Agent> agents;
|
||||
|
||||
public Agency getAgency() {
|
||||
return agency;
|
||||
}
|
||||
|
||||
public List<Agent> getAgents() {
|
||||
return agents;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.ForeignKey;
|
||||
import androidx.room.Index;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import static androidx.room.ForeignKey.CASCADE;
|
||||
|
||||
//Child class of Agency (indeed, an agent work only in one agency)
|
||||
@Entity(tableName = "agent",
|
||||
indices = {@Index(value = {"agent_first_name", "agent_last_name"}, unique = true)},
|
||||
foreignKeys = @ForeignKey(entity = Agency.class,
|
||||
parentColumns = "agency_id",
|
||||
childColumns = "agency_employer_id",
|
||||
onDelete = CASCADE))
|
||||
public class Agent {
|
||||
|
||||
@ColumnInfo(name = "agent_id")
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
public int agentId;
|
||||
|
||||
@ColumnInfo(name = "agent_first_name")
|
||||
public String agentFirstName;
|
||||
|
||||
@ColumnInfo(name = "agent_last_name")
|
||||
public String agentLastName;
|
||||
|
||||
@ColumnInfo(name = "agency_employer_id", index = true)
|
||||
public int agencyEmployerId;
|
||||
|
||||
public Agent(String agentFirstName, String agentLastName, int agencyEmployerId)
|
||||
{
|
||||
agentId = 0;
|
||||
this.agentFirstName = agentFirstName;
|
||||
this.agentLastName = agentLastName;
|
||||
this.agencyEmployerId = agencyEmployerId;
|
||||
}
|
||||
|
||||
public String getAgentName()
|
||||
{
|
||||
return agentFirstName;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.Index;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
@Entity(tableName = "property", indices = {@Index(value = {"address","longitude", "latitude"}, unique = true)})
|
||||
public class Property {
|
||||
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "property_id")
|
||||
public int propertyId;
|
||||
|
||||
@ColumnInfo(name = "description")
|
||||
public String description;
|
||||
|
||||
@ColumnInfo(name = "address")
|
||||
public String address;
|
||||
|
||||
@ColumnInfo(name = "longitude")
|
||||
public double longitude;
|
||||
|
||||
@ColumnInfo(name = "latitude")
|
||||
public double latitude;
|
||||
|
||||
public Property(String description, String address, double longitude, double latitude)
|
||||
{
|
||||
propertyId = 0;
|
||||
this.description = description;
|
||||
this.address = address;
|
||||
this.longitude = longitude;
|
||||
this.latitude = latitude;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.ForeignKey;
|
||||
import androidx.room.PrimaryKey;
|
||||
import androidx.room.TypeConverter;
|
||||
|
||||
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{
|
||||
|
||||
@ColumnInfo(name = "statue_id")
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
public int statueId;
|
||||
|
||||
@ColumnInfo(name = "statue_description")
|
||||
public boolean statueDescription;
|
||||
|
||||
@ColumnInfo(name = "sold_date")
|
||||
@Nullable
|
||||
public Date soldDate;
|
||||
|
||||
@ColumnInfo(name = "price")
|
||||
public double price;
|
||||
|
||||
@ColumnInfo(name = "belongs_property_id")
|
||||
public int propertyId;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package fr.romanet.vj.apps.myrealestateagency.entities;
|
||||
|
||||
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 {
|
||||
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "type_id")
|
||||
public int typeId;
|
||||
|
||||
@ColumnInfo(name = "type_description")
|
||||
public String typeDescription;
|
||||
|
||||
@ColumnInfo(name = "number_rooms")
|
||||
public int numberRooms;
|
||||
|
||||
@ColumnInfo(name = "surface_area")
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
@ -15,4 +16,16 @@
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonZ"
|
||||
android:layout_width="95dp"
|
||||
android:layout_height="56dp"
|
||||
android:layout_marginBottom="204dp"
|
||||
android:onClick="insertSingleTodo"
|
||||
android:text="Button"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="@+id/textView"
|
||||
app:layout_constraintHorizontal_bias="0.526"
|
||||
app:layout_constraintStart_toStartOf="@+id/textView" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Loading…
Reference in New Issue
Block a user