@Override
  public ReturnCode save(final Item item, final boolean careDependency) {
    ReturnCode rc = new ReturnCode();
    RepositoryWorkUnit<Object> repositoryWorkUnit =
        new RepositoryWorkUnit<Object>("save DQRule item") { // $NON-NLS-1$

          @Override
          protected void run() throws LoginException, PersistenceException {

            TDQBusinessRuleItem ruleItem = (TDQBusinessRuleItem) item;
            DQRule rule = ruleItem.getDqrule();
            // MOD yyi 2012-02-07 TDQ-4621:Update dependencies(connection) when careDependency is
            // true.
            if (careDependency) {
              saveWithDependencies(ruleItem, rule);
            } else {
              saveWithoutDependencies(ruleItem, rule);
            }
          }
        };
    repositoryWorkUnit.setAvoidUnloadResources(true);
    ProxyRepositoryFactory.getInstance().executeRepositoryWorkUnit(repositoryWorkUnit);
    try {
      repositoryWorkUnit.throwPersistenceExceptionIfAny();
    } catch (PersistenceException e) {
      log.error(e, e);
      rc.setOk(Boolean.FALSE);
      rc.setMessage(e.getMessage());
    }
    return rc;
  }
  /**
   * Test method for {@link
   * org.talend.dq.writer.AElementPersistance#saveWithDependencies(org.talend.core.model.properties.Item,
   * orgomg.cwm.objectmodel.core.ModelElement)} .
   *
   * @throws PersistenceException
   */
  @Test
  public void testSaveWithDependenciesAnalysis() throws PersistenceException {
    PowerMockito.mockStatic(ProxyRepositoryFactory.class);
    ProxyRepositoryFactory mockProxyRepositoryFactory =
        PowerMockito.mock(ProxyRepositoryFactory.class);
    when(ProxyRepositoryFactory.getInstance()).thenReturn(mockProxyRepositoryFactory);
    stub(
        method(
            ProxyRepositoryFactory.class,
            "save",
            org.talend.core.model.properties.Item.class,
            boolean.class)); // $NON-NLS-1$

    PowerMockito.mockStatic(IRepositoryFactory.class);
    IRepositoryFactory mockIRepositoryFactory = PowerMockito.mock(IRepositoryFactory.class);
    when(mockProxyRepositoryFactory.getRepositoryFactoryFromProvider())
        .thenReturn(mockIRepositoryFactory);

    PowerMockito.mockStatic(XmiResourceManager.class);
    when(mockIRepositoryFactory.getResourceManager()).thenReturn(new XmiResourceManager());

    AnalysisWriter createAnalysisWrite = ElementWriterFactory.getInstance().createAnalysisWrite();
    Analysis createAnalysis = AnalysisFactory.eINSTANCE.createAnalysis();
    AnalysisResult createAnalysisResult = AnalysisFactory.eINSTANCE.createAnalysisResult();
    AnalysisContext createAnalysisContext = AnalysisFactory.eINSTANCE.createAnalysisContext();
    TDQAnalysisItem analysisItem =
        org.talend.dataquality.properties.PropertiesFactory.eINSTANCE.createTDQAnalysisItem();
    analysisItem.setAnalysis(createAnalysis);
    String anaName = "ana1"; // $NON-NLS-1$
    String exceptedFileName = anaName + "_0.1.ana"; // $NON-NLS-1$
    createAnalysis.setName(anaName);
    createAnalysis.setResults(createAnalysisResult);
    createAnalysis.setContext(createAnalysisContext);

    ReturnCode create = createAnalysisWrite.saveWithDependencies(analysisItem, createAnalysis);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName, analysisItem.getFilename());

    String anaName2 = "ana2"; // $NON-NLS-1$
    String exceptedFileName2 = anaName2 + "_0.1.ana"; // $NON-NLS-1$
    createAnalysis.setName(anaName2);

    create = createAnalysisWrite.saveWithDependencies(analysisItem, createAnalysis);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName2, analysisItem.getFilename());
  }
  /**
   * Test method for {@link
   * org.talend.dq.writer.AElementPersistance#saveWithoutDependencies(org.talend.core.model.properties.Item,
   * orgomg.cwm.objectmodel.core.ModelElement)} .
   *
   * @throws PersistenceException
   */
  @Test
  public void testSaveWithoutDependenciesReport() throws PersistenceException {
    PowerMockito.mockStatic(ProxyRepositoryFactory.class);
    ProxyRepositoryFactory mockProxyRepositoryFactory =
        PowerMockito.mock(ProxyRepositoryFactory.class);
    when(ProxyRepositoryFactory.getInstance()).thenReturn(mockProxyRepositoryFactory);
    stub(
        method(
            ProxyRepositoryFactory.class,
            "save",
            org.talend.core.model.properties.Item.class,
            boolean.class)); // $NON-NLS-1$

    PowerMockito.mockStatic(IRepositoryFactory.class);
    IRepositoryFactory mockIRepositoryFactory = PowerMockito.mock(IRepositoryFactory.class);
    when(mockProxyRepositoryFactory.getRepositoryFactoryFromProvider())
        .thenReturn(mockIRepositoryFactory);

    PowerMockito.mockStatic(XmiResourceManager.class);
    when(mockIRepositoryFactory.getResourceManager()).thenReturn(new XmiResourceManager());

    ReportWriter createReportWriter = ElementWriterFactory.getInstance().createReportWriter();
    TdReport createTdReport = ReportsFactory.eINSTANCE.createTdReport();
    TDQReportItem reportItem =
        org.talend.dataquality.properties.PropertiesFactory.eINSTANCE.createTDQReportItem();
    reportItem.setReport(createTdReport);

    String repName = "rep1"; // $NON-NLS-1$
    String exceptedFileName = repName + "_0.1.rep"; // $NON-NLS-1$
    createTdReport.setName(repName);

    ReturnCode create = createReportWriter.saveWithoutDependencies(reportItem, createTdReport);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName, reportItem.getFilename());

    String repName2 = "rep2"; // $NON-NLS-1$
    String exceptedFileName2 = repName2 + "_0.1.rep"; // $NON-NLS-1$
    createTdReport.setName(repName2);

    create = createReportWriter.saveWithDependencies(reportItem, createTdReport);
    assertTrue(create.isOk());
    Assert.assertEquals(exceptedFileName2, reportItem.getFilename());
  }
  public static TypedReturnCode<Connection> createConnection(DBConnectionParameter parameter) {
    TypedReturnCode<Connection> tReturnCode = new TypedReturnCode<Connection>(false);
    MetadataFillFactory instance = null;
    instance = MetadataFillFactory.getDBInstance();

    IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(parameter));
    ReturnCode rc = instance.createConnection(metaConnection);
    if (rc.isOk()) {
      Connection dbConn = instance.fillUIConnParams(metaConnection, null);
      DatabaseMetaData dbMetadata = null;
      List<String> packageFilter = ConnectionUtils.getPackageFilter(parameter);
      java.sql.Connection sqlConn = null;
      try {
        if (rc instanceof TypedReturnCode) {
          @SuppressWarnings("rawtypes")
          Object sqlConnObject = ((TypedReturnCode) rc).getObject();
          if (sqlConnObject instanceof java.sql.Connection) {
            sqlConn = (java.sql.Connection) sqlConnObject;
            dbMetadata = org.talend.utils.sql.ConnectionUtils.getConnectionMetadata(sqlConn);
          }
        }

        instance.fillCatalogs(dbConn, dbMetadata, packageFilter);
        instance.fillSchemas(dbConn, dbMetadata, packageFilter);

        tReturnCode.setObject(dbConn);
      } catch (SQLException e) {
        log.error(e, e);
        // Need to add a dialog for report the reson of error
      } finally {
        if (sqlConn != null) {
          ConnectionUtils.closeConnection(sqlConn);
        }
      }
    } else {
      tReturnCode.setMessage(rc.getMessage());
      tReturnCode.setOk(false);
    }
    return tReturnCode;
  }
  /**
   * DOC scorreia Comment method "main".
   *
   * @param args
   */
  public static void main(String[] args) {
    TypedProperties connectionParams =
        PropertiesLoader.getProperties(IndicatorEvaluator.class, "db.properties");
    String driverClassName = connectionParams.getProperty("driver");
    String dbUrl = connectionParams.getProperty("url");
    try {
      // create connection
      java.sql.Connection connection =
          ConnectionUtils.createConnection(dbUrl, driverClassName, connectionParams);

      Connection dataProvider = new AnalysisCreationTest().getDataManager();

      // --- test connection evaluator
      String catalog = "test";

      ConnectionEvaluator evaluator = new ConnectionEvaluator();
      evaluator.setConnection(connection);

      // --- create indicators
      ConnectionIndicator connectionIndicator = SchemaFactory.eINSTANCE.createConnectionIndicator();
      evaluator.storeIndicator(dataProvider, connectionIndicator);

      // SchemaIndicator schemaIndic = SchemaFactory.eINSTANCE.createSchemaIndicator();
      // evaluator.storeIndicator(new CatalogSchema(catalog, null), schemaIndic);
      String sql = createSql(catalog);
      ReturnCode rc = evaluator.evaluateIndicators(sql, false);
      if (!rc.isOk()) {
        System.err.println("Failed to evaluate indicator: " + rc.getMessage());
      }

      // store in file
      File file = new File("out/myi." + IndicatorsPackage.eNAME);
      EMFUtil util = new EMFUtil();
      util.setUsePlatformRelativePath(false);
      if (!util.addPoolToResourceSet(file.toURI().toString(), connectionIndicator)) {
        System.err.println(util.getLastErrorMessage());
      }

      File dp = new File("out/dp.prv");
      // util.addPoolToResourceSet(new File("out/dp.prv"), dataProvider);
      util.addPoolToResourceSet(dp, dataProvider);
      List<Catalog> tdCatalogs = ConnectionHelper.getCatalogs(dataProvider);
      for (Catalog tdCatalog : tdCatalogs) {
        util.addPoolToResourceSet(
            new File("out/" + tdCatalog.getName() + "." + FactoriesUtil.CAT), tdCatalog);
      }
      util.save();
      ConnectionUtils.closeConnection(connection);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      log.error(e, e);
    }
  }