OData 4.0 Services (CRUD) using Olingo(JSON supported): Part-3
4)Create a class ‘Storage.java’
- a. readEntitySetDataFromDatabase
/* This method is called from readEntityCollection
* It is used to fetch the list of students /schools from database
* depending on the entityset provided
*/
public EntityCollection readEntitySetDataFromDatabase(EdmEntitySet edmEntitySet)
{
EntityCollection entityCollection = new EntityCollection();
if (edmEntitySet.getName().equals(DemoEdmProvider.ES_STUDENTS_NAME)) {
// entityCollection = getStudents();
List
Entity entity =null;
for(SchoolDo Do:SchoolDoList)
{
for(StudentDo a:Do.getStudentsDo())
{
entity = new Entity();
entity.addProperty(new Property(null, “stu_id”, ValueType.PRIMITIVE, a.getStu_id()));
entity.addProperty(new Property(null, “stu_name”, ValueType.PRIMITIVE,a.getStu_name()));
entity.setType(DemoEdmProvider.ET_STUDENT_FQN.getFullQualifiedNameAsString());
entity.setId(createId(entity, “stu_id”));
// schoolList.add(entity);
entityCollection.getEntities().add(entity);
}
System.out.println(“testing1 students”);
}
}
else if (edmEntitySet.getName().equals(DemoEdmProvider.ES_SCHOOLS_NAME)) {
List
Entity entity =null;
for(SchoolDo Do:SchoolDoList)
{
entity = new Entity();
entity.addProperty(new Property(null, “school_id”, ValueType.PRIMITIVE, Do.getSchool_id()));
entity.addProperty(new Property(null, “school_name”, ValueType.PRIMITIVE,Do.getSchoolName()));
entity.setType(DemoEdmProvider.ET_SCHOOL_FQN.getFullQualifiedNameAsString());
entity.setId(createId(entity, “school_name”));
System.out.println(entity.getId());
// schoolList.add(entity);
entityCollection.getEntities().add(entity);
System.out.println(“testing1 schools”);
}
//System.out.println(“hai“);
//schoolList);
}
return entityCollection;
}
- b. readEntityData
/* This method is called from readEntity or updateEntity
* */
public Entity readEntityData(EdmEntitySet edmEntitySet, List
Entity entity = null;
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
entity =readEntityDataFromDatabase(edmEntitySet, keyParams);
return entity;
}
- c. readEntitySetDataFromDatabase
/* This method is called from readEntityData
* */
public Entity readEntityDataFromDatabase(EdmEntitySet edmEntitySet, List
Entity entity = new Entity();
EdmEntityType edmEntityType = edmEntitySet.getEntityType();
if (edmEntityType.getName().equals(DemoEdmProvider.ET_STUDENT_NAME)) {
entity = getStudent(edmEntityType, keyParams);
} else if (edmEntityType.getName().equals(DemoEdmProvider.ET_SCHOOL_NAME)) {
String school_name=null;
for(UriParameter a:keyParams)
{
school_name=a.getText().replaceAll(“‘”, “”);
}
List
for(SchoolDo Do:SchoolDoList)
{
if(Do.getSchoolName()!=null){
if(Do.getSchoolName().equalsIgnoreCase(school_name))
{
entity.addProperty(new Property(null, “school_id”, ValueType.PRIMITIVE, Do.getSchool_id()));
entity.addProperty(new Property(null, “school_name”, ValueType.PRIMITIVE,Do.getSchoolName()));
entity.setType(DemoEdmProvider.ET_SCHOOL_FQN.getFullQualifiedNameAsString());
entity.setId(createId(entity, “school_id”));
}}
}
}
return entity;
}
- d. getStudent
/* This method is called from readEntitySetDataFromDatabase
This method for eg..fetches the required student from the list of students from database */
private Entity getStudent(EdmEntityType edmEntityType, List
String stu_id=null;
// the list of entities at runtime
for(UriParameter w:keyParams)
{
stu_id = w.getText();
}
EntityCollection entityCollection = getStudentsFromDatabase(Integer.parseInt(stu_id));
/* generic approach to find the requested entity */
return Util.findEntity(edmEntityType, entityCollection, keyParams);
}
- e. getStudentsFromDatabase
/* This method is called from getStudent
This method returns all the students from database */
private EntityCollection getStudentsFromDatabase(int stu_id)
{
Entity entity=null;
EntityCollection collect=new EntityCollection();
Session session = MySessionFactory.getConnection().openSession();
SchoolDto dto=new SchoolDto();
//List
Criteria c= session.createCriteria(StudentDo.class);
c.add(Restrictions.eq(“stu_id”, stu_id));
List
for(StudentDo ldo:list)
{
entity=new Entity();
entity.addProperty(new Property(null, “stu_id”, ValueType.PRIMITIVE, ldo.getStu_id()));
entity.addProperty(new Property(null, “stu_name”, ValueType.PRIMITIVE, ldo.getStu_name()));
entity.addProperty(new Property(null, “schoolIdRef”, ValueType.PRIMITIVE, ldo.getSchoolIdRef()));
entity.setType(DemoEdmProvider.ET_STUDENT_FQN.getFullQualifiedNameAsString());
entity.setId(createId(entity, “stu_id”));
collect.getEntities().add(entity);
}
return collect;
}
- f. getRelatedEntity
/*This method is called from applyExpandQueryOption
which in turn calls getRelatedEntityCollection
* */
public Entity getRelatedEntity(Entity entity, EdmEntityType relatedEntityType) {
EntityCollection collection = getRelatedEntityCollection(entity, relatedEntityType);
if (collection.getEntities().isEmpty()) {
return null;
}
return collection.getEntities().get(0);