public static List<RegisterContextRequest> getRegByEntityID(
      final EntityId reqEntityId, final ArrayList<String> discAttrList) {

    List<RegisterContextRequest> results;

    String reqEId = reqEntityId.getId();

    if (reqEId.contentEquals(".*") && reqEntityId.isIsPattern()) {
      System.out.println("\".*\" specified");
      return getRegByEntityType(reqEntityId.getType());
    }

    System.out.println("\".*\" NOT specified");

    results =
        db.query(
            new Predicate<RegisterContextRequest>() {
              public boolean match(RegisterContextRequest req) {

                final int contRegListSize = req.getContextRegistration().size();

                for (int i = 0; i < contRegListSize; i++) {

                  final int reqEIdListSize =
                      req.getContextRegistration().get(i).getEntityId().size();

                  for (int j = 0; j < reqEIdListSize; j++) {

                    String storedId =
                        req.getContextRegistration().get(i).getEntityId().get(j).getId();

                    String storedIdType =
                        req.getContextRegistration().get(i).getEntityId().get(j).getType();

                    //                        String reqEId = reqEntityId.getId();
                    boolean reqIsPattern; // = false;
                    String reqIdType = "";

                    try {
                      if (reqEntityId.getType() != null) {
                        reqIdType = reqEntityId.getType();
                      }
                    } catch (NullPointerException npe) {
                      System.out.println("'type' field missing in request");
                    }
                    if (!reqIdType.isEmpty()) {

                      if (!reqIdType.equals(storedIdType)) {
                        continue;
                      }
                    }

                    try {
                      reqIsPattern = reqEntityId.isIsPattern();
                    } catch (NullPointerException npe) {
                      System.out.println("'isPattern' field missing in request");
                      reqIsPattern = false;
                    }

                    List<ContextRegistrationAttribute> contRegAttrlist =
                        req.getContextRegistration().get(i).getContextRegistrationAttribute();

                    boolean attrMatch = checkAttrMatch(contRegAttrlist, discAttrList);

                    if (reqIsPattern) {
                      if (reqEId.contains("*")) {
                        if (reqEId.startsWith("*")) {
                          String[] suffix = reqEId.split("\\*");
                          //                                       System.out.println("ends with:
                          // "+suffix[1]);
                          //                                       System.out.println("stored:
                          // "+storedId);
                          if ((storedId.endsWith(suffix[1]) && reqIdType.isEmpty())
                              || (storedId.endsWith(suffix[1])
                                  && storedIdType.equals(reqEntityId.getType()))) {
                            System.out.println("'isPattern'");
                            return true;
                          }
                        } else if (reqEId.endsWith(".*")) {
                          String[] suffix = reqEId.split("\\.*");
                          // System.out.println(suffix[0]);
                          if ((storedId.startsWith(suffix[0]) && reqIdType.isEmpty())
                              || (storedId.startsWith(suffix[0])
                                  && storedIdType.equals(reqEntityId.getType()))) {
                            return true;
                          }
                        }
                      }
                    } else {
                      if (reqIdType.isEmpty() && storedId.equals(reqEntityId.getId())) {
                        return true;
                      } else if (storedIdType.equals(reqEntityId.getType())
                          && storedId.equals(reqEntityId.getId())) {
                        return true;
                      }
                    } // if not regIsPattern
                  }
                } // loop until true or end
                return false;
              }
            });
    return results;
  }
  // MODIFICATO
  // delete teh contReg inside the RegContReq that match the
  // with one of the entityId inside teh entityIdList
  public static void deleteRegistrationEntID(final List<String> entityIdList) {

    // for each entId
    for (String entId : entityIdList) {

      List<RegisterContextRequest> results;

      final String updateEntId = entId;

      // results contains the RegContReq that contains in their list at
      // least one contReg that have an entId that match with updateEntId
      results =
          db.query(
              new Predicate<RegisterContextRequest>() {
                public boolean match(RegisterContextRequest req) {

                  // check in the contRegList if there is one
                  // with the entId that match the updateEntId

                  List<ContextRegistration> contRegList = req.getContextRegistration();

                  // for each contReg
                  for (ContextRegistration cr : contRegList) {

                    // for each entId in contReg
                    for (EntityId entId : cr.getEntityId()) {

                      if (entId.getId().contentEquals(updateEntId)) return true;
                    }
                  }

                  return false;
                }
              });

      if (results.size() > 0) {

        // for each RegContReq find out which
        // contReg hash the entityId that match
        // with updateEntId and remove it
        // store a backup copy of that
        // RegContReq because it must be delete
        // before writing the new one
        for (RegisterContextRequest regContReq : results) {

          // check in the contRegList if there is one
          // with the entId that match the updateEntId

          List<ContextRegistration> contRegList = regContReq.getContextRegistration();

          // for each contReg
          for (ContextRegistration cr : contRegList) {

            // for each entId in contReg
            for (EntityId id : cr.getEntityId()) {

              if (id.getId().contentEquals(updateEntId)) {

                // delete the RegisterContextReq with matching entId
                RegisterContextRequest found = regContReq;
                System.out.println("Deleting registration");
                db.delete(found);
                db.commit();

                // delete the contReg, inside the RegContReq, that have a
                // matching entId
                regContReq.getContextRegistration().remove(cr);

                // if the RegisterContextRequest is not empty
                // it must be re-store the contRegs inside
                // the RegisterContextRequest that haven't a match
                // with updateEntId
                if (!regContReq.getContextRegistration().isEmpty()) {

                  storeRegistration(regContReq);
                }
              }
            }

            if (contRegList.isEmpty()) {
              break;
            }
          }
        }
      }
    }
  }