/*
   * (non-Javadoc)
   * @see org.societies.api.internal.privacytrust.privacyprotection.IPrivacyAgreementManager#getAgreement(org.societies.api.identity.Requestor)
   */
  @Override
  public AgreementEnvelope getAgreement(Requestor requestor) throws PrivacyException {
    // -- Verify
    if (null == requestor || null == requestor.getRequestorId()) {
      throw new PrivacyException(
          "Not enought information to search a privacy policy agreement. Requestor needed.");
    }
    // Dependency injection not ready
    if (!isDepencyInjectionDone()) {
      throw new PrivacyException("[Dependency Injection] PrivacyPolicyAgreementManager not ready");
    }

    try {
      List<CtxIdentifier> agreementIdList =
          ctxBroker
              .lookup(
                  CtxModelType.ATTRIBUTE, PrivacyAgreementManagerInternal.getRequestorId(requestor))
              .get();
      if (null == agreementIdList || agreementIdList.size() <= 0) {
        return null;
      }
      CtxIdentifier agreementId = agreementIdList.get(0);
      CtxAttribute agreementData = (CtxAttribute) ctxBroker.retrieve(agreementId).get();
      org.societies.api.internal.schema.privacytrust.privacyprotection.model.privacypolicy
              .AgreementEnvelope
          agreementTmp =
              (org.societies.api.internal.schema.privacytrust.privacyprotection.model.privacypolicy
                      .AgreementEnvelope)
                  SerialisationHelper.deserialise(
                      agreementData.getBinaryValue(),
                      this.getClass().getClassLoader()); // ClassLoader.getSystemClassLoader());
      if (null == agreementTmp) {
        throw new NullPointerException("NullPointerException Deserialized agreement is null");
      }
      AgreementEnvelope agreement =
          AgreementEnvelopeUtils.toAgreementEnvelope(agreementTmp, commManager.getIdManager());
      return agreement;
    } catch (CtxException e) {
      LOG.error("[Error getAgreement] Can't find the agreement. Context error.", e);
    } catch (IOException e) {
      LOG.error("[Error getAgreement] Can't find the agreement. IO error.", e);
    } catch (ClassNotFoundException e) {
      LOG.error("[Error getAgreement] Can't find the agreement. ClassNotFound error.", e);
    } catch (InterruptedException e) {
      LOG.error("[Error getAgreement] Can't find the agreement.", e);
    } catch (ExecutionException e) {
      LOG.error("[Error getAgreement] Can't find the agreement.", e);
    } catch (InvalidFormatException e) {
      LOG.error(
          "[Error getAgreement] Can't transform the agreement into a bean for serialization.", e);
    }
    return null;
  }
  public RelevantCommunityAnalyser(IIdentity linkedEntity, String linkType) {
    if (linkType.equals("CSS")) this.linkedCss = linkedEntity;
    // else
    //	this.linkedDomain = linkedEntity;

    proximityHistory = new ArrayList<ProximityRecord>();
    recordedMetadata = new HashMap<String, String>();
    refusals = new ArrayList<ICisProposal>();

    try {
      List<CtxIdentifier> ctxMetadata =
          userContextBroker.lookup(CtxModelType.ATTRIBUTE, "hasCLM").get();

      for (int i = 0; i < ctxMetadata.size(); i++) {
        CtxAttribute thisMetadata =
            (CtxAttribute) userContextBroker.retrieve(ctxMetadata.get(i)).get();
        String thisMetadataValue = thisMetadata.getStringValue();
        recordedMetadata.put(
            thisMetadataValue.split("---")[0].split("CIS ID: ")[1],
            thisMetadataValue.split("---")[1]);
      }
    } catch (InterruptedException e) {

      e.printStackTrace();
    } catch (ExecutionException e) {

      e.printStackTrace();
    } catch (CtxException e) {

      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    }

    // new ProximityRecordingThread().start();
  }
  @Before
  public void setUp() {
    this.userId = new MyIdentity(IdentityType.CSS, "xcmanager", "societies.local");
    CtxEntityIdentifier ctxId = new CtxEntityIdentifier(userId.getJid(), "Person", new Long(1));
    this.userCtxEntity = new CtxEntity(ctxId);
    hasPrivacyPreferences =
        new CtxAssociation(
            new CtxAssociationIdentifier(
                userId.getJid(), CtxTypes.HAS_PRIVACY_PREFERENCES, new Long(3)));
    CtxEntityIdentifier preferenceEntityId_ppnp1 =
        new CtxEntityIdentifier(userId.getJid(), CtxTypes.PRIVACY_PREFERENCE, new Long(2));
    this.privacyPreferenceEntity = new CtxEntity(preferenceEntityId_ppnp1);
    this.ppnp_1_CtxAttribute =
        new CtxAttribute(
            new CtxAttributeIdentifier(
                this.privacyPreferenceEntity.getId(), "ppnp_preference_1", new Long(5)));
    this.registryCtxAttribute =
        new CtxAttribute(
            new CtxAttributeIdentifier(
                userCtxEntity.getId(), CtxTypes.PRIVACY_PREFERENCE_REGISTRY, new Long(1)));
    Mockito.when(commsManager.getIdManager()).thenReturn(identityManager);
    privPrefMgr.setCtxBroker(ctxBroker);
    privPrefMgr.setCommsMgr(commsManager);
    privPrefMgr.setprivacyDataManagerInternal(privacyDataManager);
    privPrefMgr.setTrustBroker(trustBroker);
    privPrefMgr.setMyMessageBox(myMsgBox);

    try {
      Mockito.when(ctxBroker.lookup(CtxModelType.ATTRIBUTE, CtxTypes.PRIVACY_PREFERENCE_REGISTRY))
          .thenReturn(new AsyncResult<List<CtxIdentifier>>(new ArrayList<CtxIdentifier>()));
      Mockito.when(ctxBroker.lookup(CtxModelType.ENTITY, CtxTypes.PRIVACY_PREFERENCE))
          .thenReturn(new AsyncResult<List<CtxIdentifier>>(new ArrayList<CtxIdentifier>()));
      Mockito.when(ctxBroker.lookup(CtxModelType.ASSOCIATION, CtxTypes.HAS_PRIVACY_PREFERENCES))
          .thenReturn(new AsyncResult<List<CtxIdentifier>>(new ArrayList<CtxIdentifier>()));
      IndividualCtxEntity weirdPerson = new IndividualCtxEntity(userCtxEntity.getId());
      Mockito.when(ctxBroker.retrieveCssOperator())
          .thenReturn(new AsyncResult<IndividualCtxEntity>(weirdPerson));

      Mockito.when(ctxBroker.createAssociation(CtxTypes.HAS_PRIVACY_PREFERENCES))
          .thenReturn(new AsyncResult<CtxAssociation>(this.hasPrivacyPreferences));
      Mockito.when(ctxBroker.createEntity(CtxTypes.PRIVACY_PREFERENCE))
          .thenReturn(new AsyncResult<CtxEntity>(privacyPreferenceEntity));
      Mockito.when(ctxBroker.createAttribute(privacyPreferenceEntity.getId(), "ppnp_preference_1"))
          .thenReturn(new AsyncResult<CtxAttribute>(ppnp_1_CtxAttribute));
      Mockito.when(
              ctxBroker.createAttribute(
                  userCtxEntity.getId(), CtxTypes.PRIVACY_PREFERENCE_REGISTRY))
          .thenReturn(new AsyncResult<CtxAttribute>(registryCtxAttribute));

      Mockito.when(
              myMsgBox.showConfirmDialog(
                  Mockito.anyString(), Mockito.anyString(), Mockito.eq(JOptionPane.YES_NO_OPTION)))
          .thenReturn(JOptionPane.YES_OPTION);
      Mockito.when(
              ctxBroker.updateAttribute(
                  Mockito.eq(ppnp_1_CtxAttribute.getId()), (Serializable) Mockito.anyObject()))
          .thenReturn(new AsyncResult<CtxAttribute>(ppnp_1_CtxAttribute));

    } catch (CtxException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    privPrefMgr.initialisePrivacyPreferenceManager();
  }