/**
  * first i delete the file later i delete the record in database
  *
  * @param walletLanguage that you're trying to delete
  * @throws CantDeleteWalletLanguageException
  * @throws LanguageNotFoundException
  */
 @Override
 public void deleteLanguage(WalletLanguage walletLanguage)
     throws CantDeleteWalletLanguageException, LanguageNotFoundException {
   try {
     String languageFileName = getLanguageFileName(walletLanguage);
     PluginTextFile pluginTextFile =
         pluginFileSystem.getTextFile(
             pluginId,
             WALLET_LANGUAGES_PATH,
             languageFileName,
             FilePrivacy.PRIVATE,
             FileLifeSpan.PERMANENT);
     pluginTextFile.delete();
     try {
       walletLanguageMiddlewareDao.deleteLanguage(walletLanguage.getId());
     } catch (CantDeleteWalletLanguageException | LanguageNotFoundException e) {
       errorManager.reportUnexpectedPluginException(
           Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
           UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
           e);
       throw e;
     }
   } catch (CantCreateFileException e) {
     throw new CantDeleteWalletLanguageException(
         CantDeleteWalletLanguageException.DEFAULT_MESSAGE, e, "Cant delete language file", "");
   } catch (FileNotFoundException e) {
     errorManager.reportUnexpectedPluginException(
         Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
         UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
         e);
     throw new LanguageNotFoundException(
         LanguageNotFoundException.DEFAULT_MESSAGE, e, "Language not found", "");
   }
 }
  /**
   * first i insert the new language in database later i create the xml file
   *
   * @param name of the new WalletLanguage
   * @param type of the new WalletLanguage
   * @param translatorPublicKey like the name says
   * @return WalletLanguage instance
   * @throws CantCreateEmptyWalletLanguageException
   */
  @Override
  public WalletLanguage createEmptyLanguage(String name, Languages type, String translatorPublicKey)
      throws CantCreateEmptyWalletLanguageException {
    UUID languageId = UUID.randomUUID();
    LanguageState state = LanguageState.DRAFT;
    Version version = new Version("1.0.0");

    WalletLanguage walletLanguage =
        new WalletLanguageMiddlewareWalletLanguage(
            languageId, languageId, name, type, state, translatorPublicKey, version);
    try {
      Language language = new Language(name, type, new Version("1.0.0"));
      saveLanguage(language, walletLanguage);
      try {
        walletLanguageMiddlewareDao.createLanguage(walletLanguage);
        return walletLanguage;
      } catch (CantCreateEmptyWalletLanguageException e) {
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
        throw e;
      }
    } catch (CantSaveLanguageException e) {
      errorManager.reportUnexpectedPluginException(
          Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
          UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
          e);
      throw new CantCreateEmptyWalletLanguageException(
          CantCreateEmptyWalletLanguageException.DEFAULT_MESSAGE, e, "Cant create language", "");
    }
  }
  /**
   * first i try to get the file and update it if i found it, i update it, if not, i create it.
   *
   * @param language class structure that you're trying to save
   * @param walletLanguage to wich belongs
   * @throws CantSaveLanguageException
   */
  @Override
  public void saveLanguage(Language language, WalletLanguage walletLanguage)
      throws CantSaveLanguageException {
    try {
      String languageXml = getLanguageXmlFromClassStructure(language);
      String languageFileName = getLanguageFileName(walletLanguage);
      try {
        PluginTextFile newFile =
            pluginFileSystem.getTextFile(
                pluginId,
                WALLET_LANGUAGES_PATH,
                languageFileName,
                FilePrivacy.PRIVATE,
                FileLifeSpan.PERMANENT);
        newFile.loadFromMedia();
        newFile.setContent(languageXml);
        newFile.persistToMedia();

      } catch (CantLoadFileException | CantPersistFileException | CantCreateFileException e) {
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
        throw new CantSaveLanguageException(
            CantSaveLanguageException.DEFAULT_MESSAGE, e, "Can't save language xml file.", "");
      } catch (FileNotFoundException fileNotFoundException) {
        try {
          PluginTextFile newFile =
              pluginFileSystem.createTextFile(
                  pluginId,
                  WALLET_LANGUAGES_PATH,
                  languageFileName,
                  FilePrivacy.PRIVATE,
                  FileLifeSpan.PERMANENT);
          newFile.setContent(languageXml);
          newFile.persistToMedia();
        } catch (CantPersistFileException | CantCreateFileException e) {
          errorManager.reportUnexpectedPluginException(
              Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
              UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
              e);
          throw new CantSaveLanguageException(
              CantSaveLanguageException.DEFAULT_MESSAGE, e, "Can't save language xml file.", "");
        }
      }
    } catch (CantGetLanguageException e) {
      errorManager.reportUnexpectedPluginException(
          Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
          UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
          e);
      throw new CantSaveLanguageException(
          CantSaveLanguageException.DEFAULT_MESSAGE, e, "Can't create language xml string.", "");
    }
  }
  /** Service Interface implementation. */
  @Override
  public void start() throws CantStartPluginException {

    this.serviceStatus = ServiceStatus.STARTED;

    // Initializing the dao object.
    try {

      if (this.dao == null) {

        this.dao =
            new PublisherIdentityDao(
                this.pluginFileSystem,
                this.pluginDatabaseSystem,
                new PublisherIdentityDatabaseFactory(this.pluginDatabaseSystem),
                this.pluginId,
                this.logManager);
        this.dao.initializeDatabase(this.pluginId);

      } else {
        this.dao.initializeDatabase(this.pluginId);
      }

    } catch (CantInitializePublisherIdentityDatabaseException e) {
      /*
       * Catch the failure.
       * */
      errorManager.reportUnexpectedPluginException(
          Plugins.BITDUBAI_WPD_PUBLISHER_IDENTITY,
          UnexpectedPluginExceptionSeverity.DISABLES_THIS_PLUGIN,
          e);
      throw new CantStartPluginException(
          "Registry failed to start", e, Plugins.BITDUBAI_WPD_PUBLISHER_IDENTITY.getKey(), "");

    } catch (Exception e) {
      /*
       * Catch the failure.
       * */
      errorManager.reportUnexpectedPluginException(
          Plugins.BITDUBAI_WPD_PUBLISHER_IDENTITY,
          UnexpectedPluginExceptionSeverity.DISABLES_THIS_PLUGIN,
          e);
      throw new CantStartPluginException(
          "Registry failed to start", e, Plugins.BITDUBAI_WPD_PUBLISHER_IDENTITY.getKey(), "");

    } finally {
      logManager.log(
          PublisherIdentityPluginRoot.getLogLevelByClass(this.getClass().getName()),
          "Plugin started...",
          _DEFAUL_STRING,
          _DEFAUL_STRING);
    }
  }
  @Override
  public WalletLanguage createNewVersion(WalletLanguage walletLanguage)
      throws CantCopyWalletLanguageException {
    UUID id = UUID.randomUUID();
    LanguageState state = LanguageState.DRAFT;

    WalletLanguage newWalletLanguage =
        new WalletLanguageMiddlewareWalletLanguage(
            id,
            walletLanguage.getLanguageId(),
            walletLanguage.getName(),
            walletLanguage.getType(),
            state,
            walletLanguage.getTranslatorPublicKey(),
            walletLanguage.getVersion());
    try {
      Language language = getLanguage(walletLanguage);
      saveLanguage(language, newWalletLanguage);
      try {
        walletLanguageMiddlewareDao.createLanguage(newWalletLanguage);
        return newWalletLanguage;
      } catch (CantCreateEmptyWalletLanguageException e) {
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
        throw new CantCopyWalletLanguageException(
            CantCopyWalletLanguageException.DEFAULT_MESSAGE,
            e,
            "Cant insert new wallet language",
            "");
      }
    } catch (CantSaveLanguageException e) {
      errorManager.reportUnexpectedPluginException(
          Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
          UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
          e);
      throw new CantCopyWalletLanguageException(
          CantCopyWalletLanguageException.DEFAULT_MESSAGE, e, "Cant create language", "");
    } catch (CantGetLanguageException | LanguageNotFoundException e) {
      errorManager.reportUnexpectedPluginException(
          Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
          UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
          e);
      throw new CantCopyWalletLanguageException(
          CantCopyWalletLanguageException.DEFAULT_MESSAGE, e, "Language not found", "");
    }
  }
 /**
  * i get the path and name of the file and i load it
  *
  * @param walletLanguage of the language you're trying to get
  * @return language class structure
  * @throws CantGetLanguageException
  * @throws LanguageNotFoundException
  */
 @Override
 public Language getLanguage(WalletLanguage walletLanguage)
     throws CantGetLanguageException, LanguageNotFoundException {
   if (walletLanguage != null) {
     try {
       String languageFileName = getLanguageFileName(walletLanguage);
       PluginTextFile pluginTextFile =
           pluginFileSystem.getTextFile(
               pluginId,
               WALLET_LANGUAGES_PATH,
               languageFileName,
               FilePrivacy.PRIVATE,
               FileLifeSpan.PERMANENT);
       pluginTextFile.loadFromMedia();
       String xml = pluginTextFile.getContent();
       Language language = new Language();
       language = (Language) XMLParser.parseXML(xml, language);
       return language;
     } catch (CantCreateFileException | CantLoadFileException e) {
       errorManager.reportUnexpectedPluginException(
           Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
           UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
           e);
       throw new CantGetLanguageException(
           CantGetLanguageException.DEFAULT_MESSAGE, e, "Cant get language", "");
     } catch (FileNotFoundException e) {
       errorManager.reportUnexpectedPluginException(
           Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
           UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
           e);
       throw new LanguageNotFoundException(
           LanguageNotFoundException.DEFAULT_MESSAGE, e, "Language not found.", "");
     }
   } else {
     throw new CantGetLanguageException(
         CantGetLanguageException.DEFAULT_MESSAGE, null, "Wallet Language is null.", "");
   }
 }
 @Override
 public Language getLanguageFromXmlString(String languageStructure)
     throws CantGetLanguageException {
   try {
     Language language = new Language();
     language = (Language) XMLParser.parseXML(languageStructure, language);
     return language;
   } catch (Exception e) {
     errorManager.reportUnexpectedPluginException(
         Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
         UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
         e);
     throw new CantGetLanguageException(
         CantGetLanguageException.DEFAULT_MESSAGE, e, "Cant get language", "");
   }
 }
 @Override
 public void updateLanguage(WalletLanguage walletLanguage)
     throws CantUpdateLanguageException, LanguageNotFoundException {
   try {
     walletLanguageMiddlewareDao.updateLanguage(walletLanguage);
   } catch (CantUpdateLanguageException | LanguageNotFoundException e) {
     errorManager.reportUnexpectedPluginException(
         Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
         UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
         e);
     throw e;
   } catch (InvalidParameterException e) {
     // TODO METODO NO IMPLEMENTADO AUN - OJO: solo INFORMATIVO de ayuda VISUAL para DEBUG -
     // Eliminar si molesta
     e.printStackTrace();
   }
 }
 @Override
 public String getLanguageXmlFromClassStructure(Language language)
     throws CantGetLanguageException {
   try {
     String xml = null;
     if (language != null) {
       xml = XMLParser.parseObject(language);
     }
     return xml;
   } catch (Exception e) {
     errorManager.reportUnexpectedPluginException(
         Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
         UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
         e);
     throw new CantGetLanguageException(
         CantGetLanguageException.DEFAULT_MESSAGE, e, "Cant get language", "");
   }
 }
 @Override
 public WalletLanguage getLanguageById(UUID id)
     throws CantGetWalletLanguageException, LanguageNotFoundException {
   try {
     return walletLanguageMiddlewareDao.findLanguageById(id);
   } catch (CantGetWalletLanguageException | LanguageNotFoundException e) {
     errorManager.reportUnexpectedPluginException(
         Plugins.BITDUBAI_WALLET_LANGUAGE_MIDDLEWARE,
         UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
         e);
     throw e;
   } catch (InvalidParameterException e) {
     // TODO METODO NO IMPLEMENTADO AUN - OJO: solo INFORMATIVO de ayuda VISUAL para DEBUG -
     // Eliminar si molesta
     e.printStackTrace();
   }
   // TODO METODO CON RETURN NULL - OJO: solo INFORMATIVO de ayuda VISUAL para DEBUG - Eliminar si
   // molesta
   return null;
 }