Commit 802e6a2f authored by Christos Tranoris's avatar Christos Tranoris
Browse files

adding

parent 8c125081
Loading
Loading
Loading
Loading
+15 −4
Original line number Diff line number Diff line
@@ -58,8 +58,11 @@ public class ResourceApiRouteBuilder extends RouteBuilder {
	@Value("${CATALOG_GET_RESOURCE_BY_ID}")
	private String CATALOG_GET_RESOURCE_BY_ID = "";

	@Value("${CATALOG_GET_RESOURCE_BY_CATEGORY}")
	private String CATALOG_GET_RESOURCE_BY_CATEGORY = "";
	@Value("${CATALOG_GET_RESOURCES_BY_CATEGORY}")
	private String CATALOG_GET_RESOURCES_BY_CATEGORY = "";

    @Value("${CATALOG_SEARCH_RESOURCES}")
    private String CATALOG_SEARCH_RESOURCES = "";

	@Value("${CATALOG_RESOURCES_OF_PARTNERS}")
	private String CATALOG_RESOURCES_OF_PARTNERS = "";
@@ -89,13 +92,21 @@ public class ResourceApiRouteBuilder extends RouteBuilder {
		.bean( resourceRepoService, "getResourceEagerAsString")
		.convertBodyTo( String.class );

		from( CATALOG_GET_RESOURCE_BY_CATEGORY )
		.log(LoggingLevel.DEBUG, log, CATALOG_GET_RESOURCE_BY_CATEGORY + " message received!")
		from( CATALOG_GET_RESOURCES_BY_CATEGORY )
		.log(LoggingLevel.DEBUG, log, CATALOG_GET_RESOURCES_BY_CATEGORY + " message received!")
		.to("log:DEBUG?showBody=true&showHeaders=true")
		.bean( resourceRepoService, "getDetailedResourcesByCategory(${header.category})")
		.marshal().json( JsonLibrary.Jackson)
		.convertBodyTo( String.class );

        from( CATALOG_SEARCH_RESOURCES )
        .log(LoggingLevel.DEBUG, log, CATALOG_SEARCH_RESOURCES + " message received!")
        .to("log:DEBUG?showBody=true&showHeaders=true")
        .bean( resourceRepoService, "searchResources(${header.text})")
        .marshal().json( JsonLibrary.Jackson)
        .convertBodyTo( String.class );
		

		from( CATALOG_UPD_RESOURCE )
		.log(LoggingLevel.DEBUG, log, CATALOG_UPD_RESOURCE + " message received!")
		.to("log:DEBUG?showBody=true&showHeaders=true")
+79 −47
Original line number Diff line number Diff line
@@ -113,8 +113,6 @@ public class ResourceRepoService {
  public List findAll(@Valid String fields, Map<String, String> allParams)
      throws UnsupportedEncodingException {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    try {
      String sql = "SELECT " + "srv.uuid as uuid," + "srv.startOperatingDate as startOperatingDate,"
          + "srv.name as name," + "srv.category as category,"
@@ -181,14 +179,12 @@ public class ResourceRepoService {
            }
          }).list();



      return mapaEntity;



    } catch (Exception e) {
      logger.error("Error finding all resources with fields: " + fields, e);
      throw new RuntimeException("Error finding all resources", e);
    } finally {
      tx.commit();
      session.close();
    }

@@ -202,8 +198,54 @@ public class ResourceRepoService {

  public List<Map<String, Object>> getResourcesByCategory(String category) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    try {
      String hql = "SELECT " +
          "r.uuid as id, " +
          "r.type as type, " +
          "r.name as name, " +
          "r.category as category, " +
          "r.description as description " +
          "FROM RIResource r WHERE r.category = :category ORDER BY r.name ASC";

      @SuppressWarnings("unchecked")
      List<Object> queryResult = session.createQuery(hql)
          .setParameter("category", category)
          .setResultTransformer(new ResultTransformer() {
            @Override
            public Object transformTuple(Object[] tuple, String[] aliases) {
              Map<String, Object> result = new LinkedHashMap<String, Object>(tuple.length);
              for (int i = 0; i < tuple.length; i++) {
                String alias = aliases[i];
                if (alias != null) {
                  if (alias.equals("type")) {
                    result.put("@type", tuple[i]);
                  } else {
                    result.put(alias, tuple[i]);
                  }
                }
              }
              return result;
            }

            @Override
            public List transformList(List collection) {
              return collection;
            }
          })
          .list();

      return (List<Map<String, Object>>) (List<?>) queryResult;

    } catch (Exception e) {
      logger.error("Error fetching resources by category: " + category, e);
      throw new RuntimeException("Error fetching resources by category: " + category, e);
    } finally {
      session.close();
    }
  }

  public List<Map<String, Object>> searchResources(String text) {
    Session session = sessionFactory.openSession();
    try {
      String sql = "SELECT " +
          "srv.uuid as uuid," +
@@ -212,11 +254,12 @@ public class ResourceRepoService {
          "srv.category as category," +
          "srv.description as description " +
          "FROM RIResource srv " +
          "WHERE srv.category = :category " +
          "WHERE srv.category LIKE :text " +
          "OR srv.name LIKE :text " +
          "ORDER BY srv.name ASC";

      List<Object> result = session.createQuery(sql)
          .setParameter("category", category)
          .setParameter("text", text)
          .setResultTransformer(new ResultTransformer() {
            @Override
            public Object transformTuple(Object[] tuple, String[] aliases) {
@@ -241,47 +284,39 @@ public class ResourceRepoService {

      return (List<Map<String, Object>>) (List<?>) result;

    } catch (Exception e) {
      logger.error("Error searching resources: " + text, e);
      throw new RuntimeException("Error searching resources: " + text, e);
    } finally {
      tx.commit();
      session.close();
    }
  }

  @Transactional
  public List<Resource> getDetailedResourcesByCategory(String categoryName) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    List<Resource> resources = new ArrayList<>();

    try {
      String hql = "FROM RIResource r WHERE r.category = :category ORDER BY r.name ASC";
      String hql = "SELECT DISTINCT r FROM RIResource r " +
          "LEFT JOIN FETCH r.relatedParty " +
          "LEFT JOIN FETCH r.note " +
          "LEFT JOIN FETCH r.resourceCharacteristic " +
          "LEFT JOIN FETCH r.resourceSpecification " +
          "LEFT JOIN FETCH r.resourceRelationship " +
          "LEFT JOIN FETCH r.attachment " +
          "LEFT JOIN FETCH r.activationFeature " +
          "WHERE r.category = :category " +
          "ORDER BY r.name ASC";

      List<Resource> result = session.createQuery(hql, Resource.class)
          .setParameter("category", categoryName)
          .list();
          .getResultList();

      for (Resource r : result) {
        Hibernate.initialize(r.getRelatedParty());
        Hibernate.initialize(r.getNote());
        Hibernate.initialize(r.getResourceCharacteristic());
        Hibernate.initialize(r.getResourceSpecification());
        Hibernate.initialize(r.getResourceRelationship());
        Hibernate.initialize(r.getAttachment());
        Hibernate.initialize(r.getActivationFeature());
        resources.add(r);
      }

      tx.commit();
      return result;
    } catch (Exception e) {
      if (tx != null && tx.isActive()) {
        tx.rollback();
      }
      logger.error("Error fetching detailed resources by category: " + categoryName, e);
      throw new RuntimeException("Error fetching detailed resources by category: " + categoryName, e);
    } finally {
      session.close();
    }

    return resources;
  }

  @Transactional
@@ -546,7 +581,6 @@ public class ResourceRepoService {
    return resource;
  }

  @Transactional
  public String getResourceEagerAsString(String id) throws JsonProcessingException {
    Resource s = this.getResourceEager(id);
    ObjectMapper mapper = new ObjectMapper();
@@ -557,17 +591,15 @@ public class ResourceRepoService {
  }


  @Transactional
  public Resource getResourceEager(String id) {
    
    Resource s = null;
    try(Session session = sessionFactory.openSession()) {
      Transaction tx = session.beginTransaction();
      s = (Resource) session.get(Resource.class, id);
    Session session = sessionFactory.openSession();
    try {
      Resource s = (Resource) session.get(Resource.class, id);
      if (s == null) {
        return this.findByUuid(id);// last resort
        return this.findByUuid(id);
      }

      // Initialize lazy collections while session is open
      Hibernate.initialize(s.getRelatedParty());
      Hibernate.initialize(s.getNote());
      Hibernate.initialize(s.getResourceCharacteristic());
@@ -576,13 +608,13 @@ public class ResourceRepoService {
      Hibernate.initialize(s.getAttachment());
      Hibernate.initialize(s.getActivationFeature());


      tx.commit();
      return s;
    } catch (Exception e) {
      e.printStackTrace();
      logger.error("Error fetching resource eagerly: " + id, e);
      return this.findByUuid(id);
    } finally {
      session.close();
    }

    return s;
  }


+2 −1
Original line number Diff line number Diff line
@@ -234,7 +234,8 @@ CATALOG_ADD_RESOURCE: "jms:queue:CATALOG.ADD.RESOURCE"
CATALOG_UPD_RESOURCE: "jms:queue:CATALOG.UPD.RESOURCE"
CATALOG_UPDADD_RESOURCE: "jms:queue:CATALOG.UPDADD.RESOURCE"
CATALOG_GET_RESOURCE_BY_ID: "jms:queue:CATALOG.GET.RESOURCE"
CATALOG_GET_RESOURCE_BY_CATEGORY: "jms:queue:CATALOG.GET.RESOURCE_BY_CATEGORY"
CATALOG_GET_RESOURCES_BY_CATEGORY: "jms:queue:CATALOG.GET.RESOURCE_BY_CATEGORY"
CATALOG_SEARCH_RESOURCES: "jms:queue:CATALOG.SEARCH.RESOURCE"
CATALOG_ADD_RESOURCESPEC: "jms:queue:CATALOG.ADD.RESOURCESPEC"
CATALOG_UPD_RESOURCESPEC: "jms:queue:CATALOG.UPD.RESOURCESPEC"
CATALOG_UPDADD_RESOURCESPEC: "jms:queue:CATALOG.UPDADD.RESOURCESPEC"
+2 −1
Original line number Diff line number Diff line
@@ -244,7 +244,8 @@ CATALOG_ADD_RESOURCE: "jms:queue:CATALOG.ADD.RESOURCE"
CATALOG_UPD_RESOURCE: "jms:queue:CATALOG.UPD.RESOURCE"
CATALOG_UPDADD_RESOURCE: "jms:queue:CATALOG.UPDADD.RESOURCE"
CATALOG_GET_RESOURCE_BY_ID: "jms:queue:CATALOG.GET.RESOURCE"
CATALOG_GET_RESOURCE_BY_CATEGORY: "jms:queue:CATALOG.GET.RESOURCE_BY_CATEGORY"
CATALOG_GET_RESOURCES_BY_CATEGORY: "jms:queue:CATALOG.GET.RESOURCE_BY_CATEGORY"
CATALOG_SEARCH_RESOURCES: "jms:queue:CATALOG.SEARCH.RESOURCE"
CATALOG_ADD_RESOURCESPEC: "jms:queue:CATALOG.ADD.RESOURCESPEC"
CATALOG_UPD_RESOURCESPEC: "jms:queue:CATALOG.UPD.RESOURCESPEC"
CATALOG_UPDADD_RESOURCESPEC: "jms:queue:CATALOG.UPDADD.RESOURCESPEC"