Populating the database

This commit is contained in:
BGbaderguet 2021-01-04 15:41:56 +01:00
parent 9138820ea1
commit c77d5e8deb
7 changed files with 191 additions and 74 deletions

View File

@ -20,12 +20,13 @@ import fr.romanet.vj.apps.myrealestateagency.dao.PropertyDao;
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.Converters;
import fr.romanet.vj.apps.myrealestateagency.entities.DollarCurrency; import fr.romanet.vj.apps.myrealestateagency.entities.DollarCurrency;
import fr.romanet.vj.apps.myrealestateagency.entities.Modified; import fr.romanet.vj.apps.myrealestateagency.entities.Modified;
import fr.romanet.vj.apps.myrealestateagency.entities.Property; import fr.romanet.vj.apps.myrealestateagency.entities.Property;
@Database(entities = {Agency.class, Agent.class, Property.class, DollarCurrency.class, Modified.class}, version = 2, exportSchema = false) @Database(entities = {Agency.class, Agent.class, Property.class, DollarCurrency.class, Modified.class}, version = 2, exportSchema = false)
@TypeConverters({Property.Converters.class}) @TypeConverters({Converters.class})
public abstract class RealEstateAgencyDatabase extends RoomDatabase { public abstract class RealEstateAgencyDatabase extends RoomDatabase {
public abstract AgentDao agentDao(); public abstract AgentDao agentDao();

View File

@ -0,0 +1,18 @@
package fr.romanet.vj.apps.myrealestateagency.entities;
import androidx.room.TypeConverter;
import java.util.Date;
public 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();
}
}

View File

@ -3,8 +3,9 @@ package fr.romanet.vj.apps.myrealestateagency.entities;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.TypeConverters;
import java.sql.Date; import java.util.Date;
@Entity(tableName = "modified", @Entity(tableName = "modified",
primaryKeys = {"id_agent", "date_modification"}) primaryKeys = {"id_agent", "date_modification"})
@ -16,6 +17,7 @@ public class Modified {
@NonNull @NonNull
@ColumnInfo(name = "date_modification") @ColumnInfo(name = "date_modification")
@TypeConverters({Converters.class})
private Date dateModification; private Date dateModification;
public Modified(@NonNull int idAgent, @NonNull Date dateModification) public Modified(@NonNull int idAgent, @NonNull Date dateModification)
@ -23,4 +25,21 @@ public class Modified {
this.idAgent = idAgent; this.idAgent = idAgent;
this.dateModification = dateModification; this.dateModification = dateModification;
} }
@NonNull
public Date getDateModification() {
return dateModification;
}
public void setDateModification(@NonNull Date dateModification) {
this.dateModification = dateModification;
}
public int getIdAgent() {
return idAgent;
}
public void setIdAgent(int idAgent) {
this.idAgent = idAgent;
}
} }

View File

@ -1,18 +1,23 @@
package fr.romanet.vj.apps.myrealestateagency.entities; package fr.romanet.vj.apps.myrealestateagency.entities;
import android.location.Address;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Embedded; import androidx.room.Embedded;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.ForeignKey; import androidx.room.ForeignKey;
import androidx.room.Index; import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import androidx.room.TypeConverter;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.sql.Date; import java.util.Date;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import fr.romanet.vj.apps.myrealestateagency.MyApp;
import fr.romanet.vj.apps.myrealestateagency.repository.MapsActivityRepository;
import static androidx.room.ForeignKey.CASCADE; import static androidx.room.ForeignKey.CASCADE;
@Entity(tableName = "property", @Entity(tableName = "property",
@ -100,46 +105,96 @@ public class Property implements Serializable {
this.propertyType = propertyType; this.propertyType = propertyType;
} }
public static class Converters { public static Property[] populatePropertyTable(){
Date date1 = null;
@TypeConverter Date date2 = null;
public static Date toDate(Long dateLong){ Date date3 = null;
return dateLong == null ? null: new Date(dateLong); Date date4 = null;
try {
date1 = new SimpleDateFormat("yyyy-MM-dd").parse("2020-10-21");
date2 = new SimpleDateFormat("yyyy-MM-dd").parse("2019-09-01");
date3 = new SimpleDateFormat("yyyy-MM-dd").parse("2018-04-11");
date4 = new SimpleDateFormat("yyyy-MM-dd").parse("2017-02-14");
} catch (ParseException e) {
e.printStackTrace();
} }
Address address1 = null;
@TypeConverter Double longitude1 = null;
public static Long fromDate(Date date){ Double latitude1 = null;
return date == null ? null : date.getTime(); Address address2 = null;
Double longitude2 = null;
Double latitude2 = null;
Address address3 = null;
Double longitude3 = null;
Double latitude3 = null;
Address address4 = null;
Double longitude4 = null;
Double latitude4 = null;
Address address5 = null;
Double longitude5 = null;
Double latitude5 = null;
Address address6 = null;
Double longitude6 = null;
Double latitude6 = null;
Address address7 = null;
Double longitude7 = null;
Double latitude7 = null;
Address address8 = null;
Double longitude8 = null;
Double latitude8 = null;
Address address9 = null;
Double longitude9 = null;
Double latitude9 = null;
try {
address1 = MapsActivityRepository.get_lat_long_from_address("3 rue du docteur audigier", MyApp.getContext());
longitude1 = address1.getLongitude();
latitude1 = address1.getLatitude();
address2 = MapsActivityRepository.get_lat_long_from_address("124 Rue Saint-Dominique", MyApp.getContext());
longitude2 = address2.getLongitude();
latitude2 = address2.getLatitude();
address3 = MapsActivityRepository.get_lat_long_from_address("14 Avenue de la Motte-Picquet", MyApp.getContext());
longitude3 = address3.getLongitude();
latitude3 = address3.getLatitude();
address4 = MapsActivityRepository.get_lat_long_from_address("11 Avenue du Dr François Arnaud", MyApp.getContext());
longitude4 = address4.getLongitude();
latitude4 = address4.getLatitude();
address5 = MapsActivityRepository.get_lat_long_from_address("45 Avenue de Gravelle", MyApp.getContext());
longitude5 = address5.getLongitude();
latitude5 = address5.getLatitude();
address6 = MapsActivityRepository.get_lat_long_from_address("11 Rue Jules Vallès", MyApp.getContext());
longitude6 = address6.getLongitude();
latitude6 = address6.getLatitude();
address7 = MapsActivityRepository.get_lat_long_from_address("2 Rue du Monastere", MyApp.getContext());
longitude7 = address7.getLongitude();
latitude7 = address7.getLatitude();
address8 = MapsActivityRepository.get_lat_long_from_address("392 Rue Paradis", MyApp.getContext());
longitude8 = address8.getLongitude();
latitude8 = address8.getLatitude();
address9 = MapsActivityRepository.get_lat_long_from_address("Rue François Taddei", MyApp.getContext());
longitude9 = address9.getLongitude();
latitude9 = address9.getLatitude();
} catch (IOException e) {
e.printStackTrace();
} }
}
public static Property[] populatePropertyTable() throws ParseException {
String sDate1="30/10/2018";
String sDate2="05/05/2018";
String sDate3="04/12/2020";
String sDate4="31/12/2020";
Date date1= (Date) new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
Date date2= (Date) new SimpleDateFormat("dd/MM/yyyy").parse(sDate2);
Date date3= (Date) new SimpleDateFormat("dd/MM/yyyy").parse(sDate3);
Date date4= (Date) new SimpleDateFormat("dd/MM/yyyy").parse(sDate4);
return new Property[]{ return new Property[]{
new Property("Amazing property of Le Chesnay", "3 rue du docteur audigier", 15.8, 17.8, 1,
new Property("Amazing property of Le Chesnay", "3 rue du docteur audigier", longitude1, latitude1, 1,
new PropertyStatue(false, null, 15.8), new PropertyType("Apartment", 3, 9.8)), new PropertyStatue(false, null, 15.8), new PropertyType("Apartment", 3, 9.8)),
new Property("Awesome house !", "124 Rue Saint-Dominique", 15.8, 17.8, 1, new Property("Awesome house !", "124 Rue Saint-Dominique", longitude2, latitude2, 1,
new PropertyStatue(true, date1, 15.8), new PropertyType("House", 3, 9.8)), new PropertyStatue(true, date1, 15.8), new PropertyType("House", 3, 9.8)),
new Property("Incredible neighbourhood", "14 Avenue de la Motte-Picquet", 15.8, 17.8, 1, new Property("Incredible neighbourhood", "14 Avenue de la Motte-Picquet", longitude3, latitude3, 1,
new PropertyStatue(false, null, 15.8), new PropertyType("House", 3, 9.8)), new PropertyStatue(false, null, 15.8), new PropertyType("House", 3, 9.8)),
new Property("The place to be", "11 Avenue du Dr François Arnaud", 15.8, 17.8, 2, new Property("The place to be", "11 Avenue du Dr François Arnaud", longitude4, latitude4, 2,
new PropertyStatue(true, date2, 15.8), new PropertyType("Apartment", 3, 9.8)), new PropertyStatue(true, date2, 15.8), new PropertyType("Apartment", 3, 9.8)),
new Property("Foreigners accepted !", "45 Avenue de Gravelle", 15.8, 17.8, 2, new Property("Foreigners accepted !", "45 Avenue de Gravelle", longitude5, latitude5, 2,
new PropertyStatue(false, null, 15.8), new PropertyType("House", 3, 9.8)), new PropertyStatue(false, null, 15.8), new PropertyType("House", 3, 9.8)),
new Property("Family place", "11 Rue Jules Vallès", 15.8, 17.8, 2, new Property("Family place", "11 Rue Jules Vallès", longitude6, latitude6, 2,
new PropertyStatue(true, date3, 15.8), new PropertyType("Apartment", 3, 9.8)), new PropertyStatue(true, date3, 15.8), new PropertyType("Apartment", 3, 9.8)),
new Property("Best district in Marseille", "2 Rue du Monastere", 15.8, 17.8, 3, new Property("Best district in Marseille", "2 Rue du Monastere", longitude7, latitude7, 3,
new PropertyStatue(false, null, 15.8), new PropertyType("House", 3, 9.8)), new PropertyStatue(false, null, 15.8), new PropertyType("House", 3, 9.8)),
new Property("Apartment near the port", "392 Rue Paradis", 15.8, 17.8, 3, new Property("Apartment near the port", "392 Rue Paradis", longitude8, latitude8, 3,
new PropertyStatue(true, date4, 15.8), new PropertyType("Apartment", 3, 9.8)), new PropertyStatue(true, date4, 15.8), new PropertyType("Apartment", 3, 9.8)),
new Property("Near the sea", "Rue François Taddei", 15.8, 17.8, 3, new Property("Near the sea", "Rue François Taddei", longitude9, latitude9, 3,
new PropertyStatue(false, null, 15.8), new PropertyType("Apartment", 3, 9.8)) new PropertyStatue(false, null, 15.8), new PropertyType("Apartment", 3, 9.8))
}; };
} }

View File

@ -2,10 +2,10 @@ 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.Embedded; import androidx.room.TypeConverters;
import java.io.Serializable; import java.io.Serializable;
import java.sql.Date; import java.util.Date;
public class PropertyStatue implements Serializable { public class PropertyStatue implements Serializable {
@ -14,6 +14,7 @@ public class PropertyStatue implements Serializable {
@ColumnInfo(name = "sold_date") @ColumnInfo(name = "sold_date")
@Nullable @Nullable
@TypeConverters({Converters.class})
public Date soldDate; public Date soldDate;
@ColumnInfo(name = "price") @ColumnInfo(name = "price")

View File

@ -16,6 +16,7 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.io.IOException;
import java.util.List; import java.util.List;
import fr.romanet.vj.apps.myrealestateagency.R; import fr.romanet.vj.apps.myrealestateagency.R;

View File

@ -9,6 +9,17 @@
tools:context=".view.EditPropertyActivity" tools:context=".view.EditPropertyActivity"
> >
<TextView
android:id="@+id/textView3"
android:layout_width="82dp"
android:layout_height="31dp"
android:layout_marginTop="24dp"
android:text="@string/price"
app:layout_constraintEnd_toStartOf="@+id/editPropertyPrice"
app:layout_constraintHorizontal_bias="0.108"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<ImageView <ImageView
android:id="@+id/avatar" android:id="@+id/avatar"
android:layout_width="105dp" android:layout_width="105dp"
@ -49,8 +60,9 @@
android:hint="@string/price" android:hint="@string/price"
android:inputType="textPersonName" android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@id/editPropertySurface" app:layout_constraintBottom_toTopOf="@id/editPropertySurface"
app:layout_constraintEnd_toEndOf="@+id/textView2" app:layout_constraintEnd_toEndOf="@+id/editPropertyNumberOfRooms"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/editPropertyNumberOfRooms"
app:layout_constraintTop_toBottomOf="@id/editPropertyNumberOfRooms" /> app:layout_constraintTop_toBottomOf="@id/editPropertyNumberOfRooms" />
<EditText <EditText
@ -61,7 +73,7 @@
android:inputType="phone" android:inputType="phone"
app:layout_constraintBottom_toTopOf="@id/editPropertyStatueSale" app:layout_constraintBottom_toTopOf="@id/editPropertyStatueSale"
app:layout_constraintEnd_toEndOf="@+id/editPropertyPrice" app:layout_constraintEnd_toEndOf="@+id/editPropertyPrice"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="@+id/editPropertyPrice"
app:layout_constraintTop_toBottomOf="@id/editPropertyPrice" /> app:layout_constraintTop_toBottomOf="@id/editPropertyPrice" />
<EditText <EditText
@ -71,8 +83,8 @@
android:hint="@string/statueSale" android:hint="@string/statueSale"
android:inputType="textPersonName" android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@id/editPropertySoldDate" app:layout_constraintBottom_toTopOf="@id/editPropertySoldDate"
app:layout_constraintEnd_toEndOf="@+id/textView2" app:layout_constraintEnd_toEndOf="@+id/editPropertySurface"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="@+id/editPropertySurface"
app:layout_constraintTop_toBottomOf="@id/editPropertySurface" /> app:layout_constraintTop_toBottomOf="@id/editPropertySurface" />
<EditText <EditText
@ -83,7 +95,7 @@
android:inputType="textPersonName" android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@id/editPropertyDescription" app:layout_constraintBottom_toTopOf="@id/editPropertyDescription"
app:layout_constraintEnd_toEndOf="@+id/editPropertyStatueSale" app:layout_constraintEnd_toEndOf="@+id/editPropertyStatueSale"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="@+id/editPropertyStatueSale"
app:layout_constraintTop_toBottomOf="@id/editPropertyStatueSale" /> app:layout_constraintTop_toBottomOf="@id/editPropertyStatueSale" />
<EditText <EditText
@ -92,13 +104,12 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/description" android:hint="@string/description"
android:inputType="textMultiLine" android:inputType="textMultiLine"
android:minLines="4"
android:lines="4" android:lines="4"
android:minLines="4"
app:layout_constraintBottom_toTopOf="@id/editPropertyButton" app:layout_constraintBottom_toTopOf="@id/editPropertyButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/editPropertySoldDate" app:layout_constraintStart_toStartOf="@+id/editPropertySoldDate"
/> app:layout_constraintTop_toBottomOf="@id/editPropertySoldDate" />
<Button <Button
android:id="@+id/editPropertyButton" android:id="@+id/editPropertyButton"
@ -127,8 +138,8 @@
<TextView <TextView
android:id="@+id/textView2" android:id="@+id/textView2"
android:layout_width="93dp" android:layout_width="104dp"
android:layout_height="52dp" android:layout_height="47dp"
android:layout_marginEnd="40dp" android:layout_marginEnd="40dp"
android:text="@string/Number_of_room" android:text="@string/Number_of_room"
app:layout_constraintBottom_toTopOf="@+id/editPropertyPrice" app:layout_constraintBottom_toTopOf="@+id/editPropertyPrice"
@ -136,39 +147,50 @@
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.47" /> app:layout_constraintVertical_bias="0.59" />
<TextView
android:id="@+id/textView3"
android:layout_width="246dp"
android:layout_height="37dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/editPropertyNumberOfRooms"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintStart_toEndOf="@+id/editPropertyPrice"
app:layout_constraintTop_toBottomOf="@+id/editPropertyNumberOfRooms" />
<TextView <TextView
android:id="@+id/textView4" android:id="@+id/textView4"
android:layout_width="238dp" android:layout_width="83dp"
android:layout_height="40dp" android:layout_height="34dp"
android:layout_marginTop="16dp" android:layout_marginTop="20dp"
android:text="TextView" android:text="@string/surface"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toStartOf="@+id/editPropertySurface"
app:layout_constraintHorizontal_bias="0.544" app:layout_constraintHorizontal_bias="0.062"
app:layout_constraintStart_toEndOf="@+id/editPropertySurface" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" /> app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView <TextView
android:id="@+id/textView5" android:id="@+id/textView5"
android:layout_width="209dp" android:layout_width="84dp"
android:layout_height="38dp" android:layout_height="33dp"
android:layout_marginBottom="16dp" android:layout_marginTop="20dp"
android:text="TextView" android:text="@string/statueSale"
app:layout_constraintBottom_toTopOf="@+id/editPropertyDescription" app:layout_constraintEnd_toStartOf="@+id/editPropertyStatueSale"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.021"
app:layout_constraintHorizontal_bias="0.298" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@+id/editPropertySoldDate" /> app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/textView6"
android:layout_width="78dp"
android:layout_height="39dp"
android:layout_marginTop="16dp"
android:text="@string/soldDate"
app:layout_constraintEnd_toStartOf="@+id/editPropertySoldDate"
app:layout_constraintHorizontal_bias="0.094"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/textView7"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="36dp"
android:text="@string/description"
app:layout_constraintEnd_toStartOf="@+id/editPropertyDescription"
app:layout_constraintHorizontal_bias="0.177"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>