Skip to Content

4)Create a class ‘Storage.java’

  1. 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<SchoolDo> SchoolDoList = dao.readAll();

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<SchoolDo> SchoolDoList = dao.readAll();

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;

}

  1. b. readEntityData

/* This method is called from readEntity or updateEntity

* */

  public Entity readEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams) {

    Entity entity = null;

    EdmEntityType edmEntityType = edmEntitySet.getEntityType();

      entity =readEntityDataFromDatabase(edmEntitySet, keyParams);

   

    return entity;

  }

  1. c. readEntitySetDataFromDatabase

/* This method is called from readEntityData

* */

public Entity readEntityDataFromDatabase(EdmEntitySet edmEntitySet, List<UriParameter> keyParams) {

       

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<SchoolDo> SchoolDoList = dao.readAll();

              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;

         }

  1. 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<UriParameter> keyParams) {

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);

  }

  1. 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<SchoolDto> listDto=new ArrayList<SchoolDto>();

              Criteria c= session.createCriteria(StudentDo.class);

              c.add(Restrictions.eq(“stu_id”, stu_id));

              List<StudentDo> list = c.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;

}

  1. 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);

  }

/*This method is called from readEntity

which in turn calls getRelatedEntityCollection

* */

public Entity getRelatedEntity(Entity entity, EdmEntityType relatedEntityType, List<UriParameter> keyPredicates) {

    EntityCollection relatedEntities = getRelatedEntityCollection(entity, relatedEntityType);

    return Util.findEntity(relatedEntityType, relatedEntities, keyPredicates);

  }

  1. g. getRelatedEntityCollection

/*This method is called from getRelatedEntity

* This method returns all the students/schools depending upon the targetEntityType

* if targetEntityType is students then it returns all the students of the

* corresponding sourceEntity(eg..schools(‘psg’))

* */

public EntityCollection getRelatedEntityCollection(Entity sourceEntity, EdmEntityType targetEntityType) {

    EntityCollection navigationTargetEntityCollection = new EntityCollection();

    Entity entity=null;

    FullQualifiedName relatedEntityFqn = targetEntityType.getFullQualifiedName();

    String sourceEntityFqn = sourceEntity.getType();

    if (sourceEntityFqn.equals(DemoEdmProvider.ET_STUDENT_FQN.getFullQualifiedNameAsString())

        && relatedEntityFqn.equals(DemoEdmProvider.ET_SCHOOL_FQN)) {

       System.out.println(“get related entity collection method”);

       navigationTargetEntityCollection.setId(createId(sourceEntity, “stu_id”, DemoEdmProvider.NAV_TO_SCHOOL));

       int stu_id = (int) sourceEntity.getProperty(“stu_id”).getValue();

      

      

       for(SchoolDto sdto:dao.readStudentData(stu_id))

       {

              entity = new Entity();

              System.out.println(“stu id is”+sdto.getSchool_id());

           entity.addProperty(new Property(null, “school_id”, ValueType.PRIMITIVE, sdto.getSchool_id()));

           entity.addProperty(new Property(null, “school_name”, ValueType.PRIMITIVE, sdto.getSchoolName()));

          

           entity.setType(DemoEdmProvider.ET_SCHOOL_FQN.getFullQualifiedNameAsString());

           entity.setId(createId(entity, “school_name”));

           navigationTargetEntityCollection.getEntities().add(entity);

       

       }

    } else if (sourceEntityFqn.equals(DemoEdmProvider.ET_SCHOOL_FQN.getFullQualifiedNameAsString())

        && relatedEntityFqn.equals(DemoEdmProvider.ET_STUDENT_FQN)) {

  1. navigationTargetEntityCollection.setId(createId(sourceEntity, “school_name”, DemoEdmProvider.NAV_TO_STUDENTS));

      // relation Category->Products (result all products)

String school_name = (String) sourceEntity.getProperty(“school_name”).getValue();

  1. System.out.println(“school name inside storage java”+school_name);

SchoolDto dto=new SchoolDto();

SchoolServiceDao dao=new SchoolServiceDao();

dto=dao.readSchoolData(school_name);

  1. System.out.println(“hee”);

for(StudentDto sdto:dto.getStudentsDto())

{

       entity = new Entity();

       System.out.println(“stu id is”+sdto.getStu_id());

    entity.addProperty(new Property(null, “stu_id”, ValueType.PRIMITIVE, sdto.getStu_id()));

    entity.addProperty(new Property(null, “stu_name”, ValueType.PRIMITIVE, sdto.getStu_name()));

   

    entity.setType(DemoEdmProvider.ET_STUDENT_FQN.getFullQualifiedNameAsString());

    entity.setId(createId(entity, “stu_id”));

    navigationTargetEntityCollection.getEntities().add(entity);

}

    else if (sourceEntityFqn.equals(DemoEdmProvider.ET_SCHOOL_FQN.getFullQualifiedNameAsString())

            && relatedEntityFqn.equals(DemoEdmProvider.ET_TEACHER_FQN)) {

       TeacherDto dto_tea=new TeacherDto();

    navigationTargetEntityCollection.setId(createId(sourceEntity, “school_name”, DemoEdmProvider.NAV_TO_TEACHERS));

          // relation Category->Products (result all products)

    String school_name = (String) sourceEntity.getProperty(“school_name”).getValue();

    System.out.println(“school name inside storage java”+school_name);

    SchoolDto dto1 = new SchoolDto();

    SchoolServiceDao dao=new SchoolServiceDao();

    dto1=dao.readSchoolData(school_name);

    System.out.println(“school dto1 size”+dto1);

    for(TeacherDto tdto:dto1.getTeachersDto())

    {

       entity = new Entity();

       System.out.println(“TEACHER id is”+tdto.getTeacher_id());

        entity.addProperty(new Property(null, “teacher_id”, ValueType.PRIMITIVE, tdto.getTeacher_id()));

        entity.addProperty(new Property(null, “teacher_name”, ValueType.PRIMITIVE, tdto.getTeacher_name()));

       

        entity.setType(DemoEdmProvider.ET_TEACHER_FQN.getFullQualifiedNameAsString());

        entity.setId(createId(entity, “teacher_id”));

        navigationTargetEntityCollection.getEntities().add(entity);

    }}

    if (navigationTargetEntityCollection.getEntities().isEmpty()) {

      return null;

    }

    return navigationTargetEntityCollection;

  }

  1. h. createId

/*This method is creates id(uri) by appending entityset name followed by navigation name

* */

public URI createId(Entity entity, String idPropertyName) {

    return createId(entity,idProperName,null);

  }

  private URI createId(Entity entity, String idPropertyName, String navigationName) {

    try {

      StringBuilder sb = new StringBuilder(getEntitySetName(entity)).append(“(“);

      final Property property = entity.getProperty(idPropertyName);

      sb.append(property.asPrimitive()).append(“)”);

      if(navigationName != null) {

        sb.append(“/”).append(navigationName);

      }

      return new URI(sb.toString());

    } catch (URISyntaxException e) {

      throw new ODataRuntimeException(“Unable to create (Atom) id for entity: “ + entity, e);

    }

  }

  1. i. getEntitySetName

/*This method is called from createId method

*Returns entitySetName for the required entity

* */

private String getEntitySetName(Entity entity) {

    if(DemoEdmProvider.ET_SCHOOL_FQN.getFullQualifiedNameAsString().equals(entity.getType())) {

      return DemoEdmProvider.ES_SCHOOLS_NAME;

    } else if(DemoEdmProvider.ET_STUDENT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) {

      return DemoEdmProvider.ES_STUDENTS_NAME;

    }

    return entity.getType();

  }

  1. j.createEntityData,createSschool,createStudent

/*This method is called from createEntity method in processor

*which inturn calls createSchool/createStudent based on entity set name

* */

public Entity createEntityData(EdmEntitySet edmEntitySet, Entity entityToCreate) {

EdmEntityType edmEntityType = edmEntitySet.getEntityType();

// actually, this is only required if we have more than one Entity Type

if (edmEntityType.getName().equals(DemoEdmProvider.ET_STUDENT_NAME)) {

return createStudent (edmEntityType,entityToCreate);

}

if (edmEntityType.getName().equals(DemoEdmProvider.ET_SCHOOL_NAME)) {

System.out.println(“school name matched”);

                    return createSchool(edmEntityType, entityToCreate);

}

return null;

         }

/*This method is called from createEntityData method

*This method gets school entity,creates new school_id and add to schools list

* */

  private Entity createSchool(EdmEntityType edmEntityType, Entity entity) {

       // TODO Auto-generated method stub

         int newId = 1;

while (schoolIdExists(newId)) {

newId++;

}

Property idProperty = entity.getProperty(“school_id”);

if (idProperty != null) {

idProperty.setValue(ValueType.PRIMITIVE, Integer.valueOf(newId));

} else {

// as of OData v4 spec, the key property can be omitted from the POST request body

entity.getProperties().add(new Property(null, “school_id”, ValueType.PRIMITIVE, newId));

}

entity.setId(createId(entity, “school_id”));

this.schoolList.add(entity);

return entity;

}

/*This method is called from createEntityData method

*This method gets student entity,creates new student_id and add to students list

* */

private Entity createStudent(EdmEntityType edmEntityType, Entity entity) {

// the ID of the newly created product entity is generated automatically

int newId = 1;

while (studentIdExists(newId)) {

newId++;

}

Property idProperty = entity.getProperty(“stu_id”);

if (idProperty != null) {

idProperty.setValue(ValueType.PRIMITIVE, Integer.valueOf(newId));

} else {

// as of OData v4 spec, the key property can be omitted from the POST request body

entity.getProperties().add(new Property(null, “stu_id”, ValueType.PRIMITIVE, newId));

}

entity.setId(createId(entity, “stu_id”));

this.studentList.add(entity);

return entity;

         }

  1. k. studentIdExists

/*This method is called from createStudent

*This method checks if the student id already exists in database

* */

private boolean studentIdExists(int id) {

for (Entity entity : this.studentList) {

Integer existingID = (Integer) entity.getProperty(“stu_id”).getValue();

if (existingID.intValue() == id) {

return true;

}

}

return false;

         }

/*This method is called from createSchool

*This method checks if the school id already exists in database

* */

private boolean schoolIdExists(int id) {

  for (Entity entity : this.schoolList) {

                    Integer existingID = (Integer) entity.getProperty(“school_id”).getValue();

                    if (existingID.intValue() == id) {

                      return true;

                    }

}

return false;

                }

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply