/**
  * Saves itself in data base. If already exists in database performs update otherwise inserts new
  * record. Returns current id if update was executed or new id in case of insert.
  *
  * @return unique id
  */
 public int save() throws DBException {
   SQLUpdate update = new SQLUpdate(TABLE_NAME);
   if (_name != null) {
     update.addValue("solution_name", _name);
   }
   if (_info != null) {
     update.addValue("solution_info", _info);
   }
   if (_host != null) {
     update.addValue("hostname", _host);
   }
   if (_path != null) {
     update.addValue("db_path", _path);
   }
   if (_user != null) {
     update.addValue("username", _user);
   }
   if (_passwd != null) {
     update.addValue("passwd", _passwd);
   }
   update.addValue("lm_date", new Date(System.currentTimeMillis()));
   try {
     _solutionID = _dbManager.insertOrUpdate(update, "solution_id", _solutionID, GEN_NAME);
   } catch (SQLException e) {
     LOGGER.fatal("Exception was thrown!", e);
     throw new DBException("Cannot save solution info!", e);
   }
   return _solutionID;
 }
 public void run() {
   LOGGER.info("Running tasks");
   /*
    * while (true) { Task task = getTask(); IDataPlugin plugin =
    * task.getPlugin(); Settings settings = task.getSettings();
    * plugin.doJob(_dataEngine, _environment, settings);
    */
   TaskInfo taskInfo = null;
   for (ITask task : _tasks) {
     try {
       taskInfo = ((Task) task).getInfo();
       LOGGER.info("task: " + taskInfo.getName());
       ISettings settings = task.getSettings();
       taskInfo.setStatus(TaskInfo.REALIZATION);
       // changing status
       taskInfo.save();
       _dbManager.commit();
       //
       // processing task
       //
       ILocalPlugin plugin = task.getPlugin();
       LOGGER.debug("plugin: " + plugin + " id: " + plugin.getInfo().getId());
       IResult result = plugin.doJob(getDataEngine(), _environment, settings);
       //
       // saving result of its execution
       //
       task.setResult(result);
       taskInfo.save();
       _dbManager.commit();
       //
       // if task was not processed correctly
       // exception is caught and shoul be handled here
       //
     } catch (Exception e) {
       LOGGER.fatal("TASK PROCESSING ERROR", e);
       try {
         taskInfo.setStatus(TaskInfo.EXCEPTION);
         taskInfo.save();
       } catch (Exception ex) {
         LOGGER.fatal("", ex);
       }
       _dbManager.commit();
     }
   }
 }
 public boolean delete() throws DBException {
   SQLDelete delete = new SQLDelete(TABLE_NAME);
   delete.addCondition("solution_id =", _solutionID);
   try {
     return (_dbManager.delete(delete) > 0);
   } catch (SQLException e) {
     LOGGER.fatal("Exception was thrown!", e);
     throw new DBException("Cannot delete solution info!", e);
   }
 }
 /** @see salomon.engine.task.ITaskManager#addTask(salomon.platform.task.ITask) */
 public void addTask(ITask task) throws PlatformException {
   try {
     task.getInfo().save();
     _dbManager.commit();
   } catch (Exception e) {
     _dbManager.rollback();
     LOGGER.fatal("", e);
     throw new PlatformException(e.getLocalizedMessage());
   }
   _tasks.add(task);
 }
 public void saveTasks() throws PlatformException {
   try {
     for (ITask task : _tasks) {
       ((Task) task).getInfo().save();
     }
     _dbManager.commit();
   } catch (Exception e) {
     _dbManager.rollback();
     LOGGER.fatal("", e);
     throw new PlatformException(e.getLocalizedMessage());
   }
 }
 /**
  * Initializes itself basing on given row from resultSet.
  *
  * @param resultSet
  * @throws DBException
  */
 public void load(ResultSet resultSet) throws DBException {
   try {
     _solutionID = resultSet.getInt("solution_id");
     _name = resultSet.getString("solution_name");
     _info = resultSet.getString("solution_info");
     _host = resultSet.getString("hostname");
     _path = resultSet.getString("db_path");
     _user = resultSet.getString("username");
     _passwd = resultSet.getString("passwd");
   } catch (SQLException e) {
     LOGGER.fatal("Exception was thrown!", e);
     throw new DBException("Cannot load solution info!", e);
   }
 }
 public void testLoad() {
   LOGGER.debug("DescriptionTest.testLoad()");
   boolean success = false;
   SQLSelect select = new SQLSelect();
   select.addTable(PluginInfo.TABLE_NAME);
   select.addCondition("plugin_id =", 20);
   ResultSet resultSet = null;
   try {
     resultSet = _manager.select(select);
     assertNotNull(resultSet);
     success = true;
   } catch (SQLException e) {
     LOGGER.fatal("", e);
   }
   assertTrue(success);
   success = false;
   // PluginInfo desc = new PluginInfo();
   try {
     if (resultSet.next()) {
       // desc.load(resultSet);
       success = true;
     } else {
       LOGGER.debug("No data found");
       success = true;
     }
   } catch (Exception e) {
     LOGGER.fatal("", e);
   } finally {
     try {
       resultSet.close();
     } catch (SQLException ex) {
       LOGGER.fatal("", ex);
     }
   }
   assertTrue(success);
   // LOGGER.debug(desc);
 }
 public void testDelete() {
   LOGGER.debug("DescriptionTest.testDelete()");
   boolean success = false;
   // PluginInfo desc = new PluginInfo();
   // desc.setPluginID(40);
   try {
     // desc.delete();
     success = true;
     _manager.commit();
   } catch (Exception e) {
     LOGGER.fatal("", e);
     _manager.rollback();
   }
   assertTrue(success);
 }
 public TaskManager(IManagerEngine managerEngine, DBManager manager) {
   _managerEngine = managerEngine;
   _dbManager = manager;
   _tasks = new LinkedList<ITask>();
   _taskEngine = new TaskEngine();
   // TODO: where it should be created?
   _environment = new Environment();
   // TODO: temporary
   // TODO: temporary
   IVariable variable;
   try {
     variable = _environment.createEmpty("CURRENT_DATA_SET");
     variable.setValue(new SimpleString("all_data"));
     _environment.add(variable);
   } catch (PlatformException e) {
     LOGGER.fatal("Exception was thrown!", e);
   }
 }
 /**
  * Removes given tasks.
  *
  * @see salomon.engine.task.ITaskManager#removeTask(salomon.engine.task.ITask)
  */
 public boolean removeTask(ITask task) throws PlatformException {
   SQLDelete delete = new SQLDelete(TaskInfo.TABLE_NAME);
   IProject currProject = _managerEngine.getProjectManager().getCurrentProject();
   delete.addCondition("project_id =", currProject.getInfo().getId());
   delete.addCondition("task_id = ", task.getInfo().getId());
   boolean retVal = false;
   int deletedRows = 0;
   try {
     deletedRows = _dbManager.delete(delete);
     _dbManager.commit();
     // removing from list
     _tasks.remove(task);
     retVal = (deletedRows > 0);
   } catch (SQLException e) {
     _dbManager.rollback();
     LOGGER.fatal("", e);
     throw new PlatformException(e.getLocalizedMessage());
   }
   return retVal;
 }
    private ITask getTask() {
      ITask currentTask = null;
      while (true) {
        synchronized (_tasks) {
          if (_tasks.size() > 0) {
            currentTask = _tasks.getFirst();
          }
        }
        _tasks.notifyAll();
        if (currentTask != null) {
          break;
        }
        try {
          _tasks.wait();
        } catch (InterruptedException e) {
          LOGGER.fatal("", e);
        }
      } // while (currentTask != null);

      return currentTask;
    }
 public void testSave() {
   LOGGER.debug("DescriptionTest.testSave()");
   boolean success = false;
   // PluginInfo desc = new PluginInfo();
   // desc.setName("test_plugin");
   // try {
   // desc.setLocation(new URL("http://www.test_description.pl"));
   //	success = true;
   // } catch (MalformedURLException e) {
   //	LOGGER.fatal("", e);
   // }
   assertTrue(success);
   success = false;
   try {
     // desc.save();
     _manager.commit();
     success = true;
   } catch (Exception e) {
     LOGGER.fatal("", e);
     _manager.rollback();
   }
   assertTrue(success);
 }
  public IResult doJob(IDataEngine dataEngine, IEnvironment env, ISettings settings) {
    CreatorSettings cSettings = (CreatorSettings) settings;
    String dataSetName = cSettings.getDataSetName();
    IDataSetManager dataSetManager = dataEngine.getDataSetManager();

    boolean isSuccessfull = false;

    try {
      String[] strConds = cSettings.getConditions();
      ICondition[] conditions = new ICondition[strConds.length];
      int i = 0;
      for (String cond : strConds) {
        if (cond != null) {
          conditions[i] = dataSetManager.createCondition(cond);
          ++i;
        }
      }

      IDataSet mainDataSet = dataSetManager.getMainDataSet();
      IDataSet subSet = mainDataSet.createSubset(conditions);
      subSet.setName(dataSetName);
      dataSetManager.add(subSet);

      isSuccessfull = true;
    } catch (PlatformException e) {
      LOGGER.fatal("", e);
    }

    CreatorResult result = new CreatorResult();
    result.setSuccessful(isSuccessfull);
    if (!isSuccessfull) {
      dataSetName = "ERROR";
    }
    result.setDataSetName(dataSetName);
    return result;
  }
  /**
   * Returns tasks associated with current project. They are returned sorted by task_nr.
   *
   * @see salomon.engine.task.ITaskManager#getTasks()
   */
  public ITask[] getTasks() throws PlatformException {
    LinkedList<ITask> tasks = null;
    // TODO: use _project in stead of it
    IProject currProject = _managerEngine.getProjectManager().getCurrentProject();
    if (_tasks.isEmpty() && currProject != null) {
      tasks = new LinkedList<ITask>();
      SQLSelect select = new SQLSelect();
      select.addTable(TaskInfo.TABLE_NAME + " t");
      select.addTable(PluginInfo.TABLE_NAME + " p");
      select.addColumn("t.task_id");
      select.addColumn("t.task_nr");
      select.addColumn("t.project_id");
      select.addColumn("t.task_name");
      select.addColumn("t.task_info");
      select.addColumn("t.status");
      select.addColumn("t.plugin_settings");
      select.addColumn("t.plugin_result");
      select.addColumn("p.plugin_id");
      select.addColumn("p.plugin_name");
      select.addColumn("p.plugin_info");
      select.addColumn("p.location");
      int projectID = currProject.getInfo().getId();
      select.addCondition("t.project_id =", projectID);
      select.addCondition("t.plugin_id = p.plugin_id");
      // tasks are sorted by task_nr
      select.addOrderBy("t.task_nr");

      // executing query
      ResultSet resultSet = null;
      try {
        resultSet = _dbManager.select(select);
        while (resultSet.next()) {
          // TODO: Plugin info shoul not be instantied from outside of pluginManger :-/
          PluginInfo pluginInfo = new PluginInfo(_dbManager);
          pluginInfo.load(resultSet);
          LocalPlugin localPlugin =
              (LocalPlugin) _managerEngine.getPluginManager().getPlugin(pluginInfo);
          localPlugin.load();
          // FIXME: real settings should be loaded
          ISettings pluginSettings = localPlugin.getSettingComponent().getDefaultSettings();

          Task task = (Task) this.createTask();
          task.setSettings(pluginSettings);
          task.setPlugin(localPlugin);
          // loading task
          task.getInfo().load(resultSet);
          tasks.add(task);
        }
        resultSet.close();
        // if everything is OK, then loaded tasks list is assigned
        // to manager's tasks list
        _tasks = tasks;
      } catch (Exception e) {
        LOGGER.fatal("", e);
        try {
          resultSet.close();
        } catch (SQLException ex) {
          LOGGER.fatal("", ex);
        }
        throw new PlatformException(e.getLocalizedMessage());
      }
    }
    ITask[] tasksArr = new ITask[_tasks.size()];
    return _tasks.toArray(tasksArr);
  }