private void uploadFlow(
      Connection connection, Project project, int version, Flow flow, EncodingType encType)
      throws ProjectManagerException, IOException {
    QueryRunner runner = new QueryRunner();
    String json = JSONUtils.toJSON(flow.toObject());
    byte[] stringData = json.getBytes("UTF-8");
    byte[] data = stringData;

    logger.info("UTF-8 size:" + data.length);
    if (encType == EncodingType.GZIP) {
      data = GZIPUtils.gzipBytes(stringData);
    }

    logger.info("Flow upload " + flow.getId() + " is byte size " + data.length);
    final String INSERT_FLOW =
        "INSERT INTO project_flows (project_id, version, flow_id, modified_time, encoding_type, json) values (?,?,?,?,?,?)";
    try {
      runner.update(
          connection,
          INSERT_FLOW,
          project.getId(),
          version,
          flow.getId(),
          System.currentTimeMillis(),
          encType.getNumVal(),
          data);
    } catch (SQLException e) {
      throw new ProjectManagerException("Error inserting flow " + flow.getId(), e);
    }
  }
  private void updateProjectSettings(Connection connection, Project project, EncodingType encType)
      throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();
    final String UPDATE_PROJECT_SETTINGS =
        "UPDATE projects SET enc_type=?, settings_blob=? WHERE id=?";

    String json = JSONUtils.toJSON(project.toObject());
    byte[] data = null;
    try {
      byte[] stringData = json.getBytes("UTF-8");
      data = stringData;

      if (encType == EncodingType.GZIP) {
        data = GZIPUtils.gzipBytes(stringData);
      }
      logger.debug(
          "NumChars: " + json.length() + " UTF-8:" + stringData.length + " Gzip:" + data.length);
    } catch (IOException e) {
      throw new ProjectManagerException("Failed to encode. ", e);
    }

    try {
      runner.update(
          connection, UPDATE_PROJECT_SETTINGS, encType.getNumVal(), data, project.getId());
      connection.commit();
    } catch (SQLException e) {
      throw new ProjectManagerException(
          "Error updating project " + project.getName() + " version " + project.getVersion(), e);
    }
  }
  private Project fetchProjectById(Connection connection, int id) throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();
    // Fetch the project
    Project project = null;
    ProjectResultHandler handler = new ProjectResultHandler();
    try {
      List<Project> projects =
          runner.query(connection, ProjectResultHandler.SELECT_PROJECT_BY_ID, handler, id);
      if (projects.isEmpty()) {
        throw new ProjectManagerException("No active project with id " + id + " exists in db.");
      }

      project = projects.get(0);
    } catch (SQLException e) {
      logger.error(ProjectResultHandler.SELECT_PROJECT_BY_ID + " failed.");
      throw new ProjectManagerException("Query for existing project failed. Project " + id, e);
    }

    // Fetch the user permissions
    List<Triple<String, Boolean, Permission>> permissions =
        fetchPermissionsForProject(connection, project);

    for (Triple<String, Boolean, Permission> perm : permissions) {
      if (perm.getThird().toFlags() != 0) {
        if (perm.getSecond()) {
          project.setGroupPermission(perm.getFirst(), perm.getThird());
        } else {
          project.setUserPermission(perm.getFirst(), perm.getThird());
        }
      }
    }

    return project;
  }
  private List<Project> fetchAllActiveProjects(Connection connection)
      throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();

    ProjectResultHandler handler = new ProjectResultHandler();
    List<Project> projects = null;
    try {
      projects = runner.query(connection, ProjectResultHandler.SELECT_ALL_ACTIVE_PROJECTS, handler);

      for (Project project : projects) {
        List<Triple<String, Boolean, Permission>> permissions =
            fetchPermissionsForProject(connection, project);

        for (Triple<String, Boolean, Permission> entry : permissions) {
          if (entry.getSecond()) {
            project.setGroupPermission(entry.getFirst(), entry.getThird());
          } else {
            project.setUserPermission(entry.getFirst(), entry.getThird());
          }
        }
      }
    } catch (SQLException e) {
      throw new ProjectManagerException("Error retrieving all projects", e);
    } finally {
      DbUtils.closeQuietly(connection);
    }

    return projects;
  }
Exemple #5
0
 public List<Project> getGroupProjects(User user) {
   List<Project> array = new ArrayList<Project>();
   for (Project project : projectsById.values()) {
     if (project.hasGroupPermission(user, Type.READ)) {
       array.add(project);
     }
   }
   return array;
 }
Exemple #6
0
  public synchronized Project removeProject(Project project, User deleter)
      throws ProjectManagerException {
    projectLoader.removeProject(project, deleter.getUserId());
    projectLoader.postEvent(project, EventType.DELETED, deleter.getUserId(), null);

    projectsByName.remove(project.getName());
    projectsById.remove(project.getId());

    return project;
  }
Exemple #7
0
 /**
  * Permanently delete all project files and properties data for all versions of a project and log
  * event in project_events table
  *
  * @param project
  * @param deleter
  * @return
  * @throws ProjectManagerException
  */
 public synchronized Project purgeProject(Project project, User deleter)
     throws ProjectManagerException {
   projectLoader.cleanOlderProjectVersion(project.getId(), project.getVersion() + 1);
   projectLoader.postEvent(
       project,
       EventType.PURGE,
       deleter.getUserId(),
       String.format("Purged versions before %d", project.getVersion() + 1));
   return project;
 }
Exemple #8
0
  public List<Project> getUserProjects(User user) {
    ArrayList<Project> array = new ArrayList<Project>();
    for (Project project : projectsById.values()) {
      Permission perm = project.getUserPermission(user);

      if (perm != null && (perm.isPermissionSet(Type.ADMIN) || perm.isPermissionSet(Type.READ))) {
        array.add(project);
      }
    }
    return array;
  }
  @Override
  public int getLatestProjectVersion(Project project) throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    IntHander handler = new IntHander();
    try {
      return runner.query(IntHander.SELECT_LATEST_VERSION, handler, project.getId());
    } catch (SQLException e) {
      logger.error(e);
      throw new ProjectManagerException(
          "Error marking project " + project.getName() + " as inactive", e);
    }
  }
Exemple #10
0
  @Override
  public ProjectFileHandler getUploadedFile(Project project, int version)
      throws ProjectManagerException {
    logger.info("Retrieving to " + project.getName() + " version:" + version);
    Connection connection = getConnection();
    ProjectFileHandler handler = null;
    try {
      handler = getUploadedFile(connection, project.getId(), version);
    } finally {
      DbUtils.closeQuietly(connection);
    }

    return handler;
  }
Exemple #11
0
  @Override
  public void removeProject(Project project, String user) throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    long updateTime = System.currentTimeMillis();
    final String UPDATE_INACTIVE_PROJECT =
        "UPDATE projects SET active=false,modified_time=?,last_modified_by=? WHERE id=?";
    try {
      runner.update(UPDATE_INACTIVE_PROJECT, updateTime, user, project.getId());
    } catch (SQLException e) {
      logger.error(e);
      throw new ProjectManagerException(
          "Error marking project " + project.getName() + " as inactive", e);
    }
  }
Exemple #12
0
  private void loadAllProjects() {
    List<Project> projects;
    try {
      projects = projectLoader.fetchAllActiveProjects();
    } catch (ProjectManagerException e) {
      throw new RuntimeException("Could not load projects from store.", e);
    }
    for (Project proj : projects) {
      projectsByName.put(proj.getName(), proj);
      projectsById.put(proj.getId(), proj);
    }

    for (Project proj : projects) {
      loadAllProjectFlows(proj);
    }
  }
Exemple #13
0
 public List<Project> getProjectsByRegex(String regexPattern) {
   List<Project> allProjects = new ArrayList<Project>();
   Pattern pattern;
   try {
     pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
   } catch (PatternSyntaxException e) {
     logger.error("Bad regex pattern " + regexPattern);
     return allProjects;
   }
   for (Project project : getProjects()) {
     if (pattern.matcher(project.getName()).find()) {
       allProjects.add(project);
     }
   }
   return allProjects;
 }
Exemple #14
0
 public void updateProjectPermission(
     Project project, String name, Permission perm, boolean group, User modifier)
     throws ProjectManagerException {
   logger.info(
       "User "
           + modifier.getUserId()
           + " updating permissions for project "
           + project.getName()
           + " for "
           + name
           + " "
           + perm.toString());
   projectLoader.updatePermission(project, name, perm, group);
   if (group) {
     projectLoader.postEvent(
         project,
         EventType.GROUP_PERMISSION,
         modifier.getUserId(),
         "Permission for group " + name + " set to " + perm.toString());
   } else {
     projectLoader.postEvent(
         project,
         EventType.USER_PERMISSION,
         modifier.getUserId(),
         "Permission for user " + name + " set to " + perm.toString());
   }
 }
Exemple #15
0
    @Override
    public List<Project> handle(ResultSet rs) throws SQLException {
      if (!rs.next()) {
        return Collections.<Project>emptyList();
      }

      ArrayList<Project> projects = new ArrayList<Project>();
      do {
        int id = rs.getInt(1);
        String name = rs.getString(2);
        boolean active = rs.getBoolean(3);
        long modifiedTime = rs.getLong(4);
        long createTime = rs.getLong(5);
        int version = rs.getInt(6);
        String lastModifiedBy = rs.getString(7);
        String description = rs.getString(8);
        int encodingType = rs.getInt(9);
        byte[] data = rs.getBytes(10);

        Project project;
        if (data != null) {
          EncodingType encType = EncodingType.fromInteger(encodingType);
          Object blobObj;
          try {
            // Convoluted way to inflate strings. Should find common package or
            // helper function.
            if (encType == EncodingType.GZIP) {
              // Decompress the sucker.
              String jsonString = GZIPUtils.unGzipString(data, "UTF-8");
              blobObj = JSONUtils.parseJSONFromString(jsonString);
            } else {
              String jsonString = new String(data, "UTF-8");
              blobObj = JSONUtils.parseJSONFromString(jsonString);
            }
            project = Project.projectFromObject(blobObj);
          } catch (IOException e) {
            throw new SQLException("Failed to get project.", e);
          }
        } else {
          project = new Project(id, name);
        }

        // update the fields as they may have changed

        project.setActive(active);
        project.setLastModifiedTimestamp(modifiedTime);
        project.setCreateTimestamp(createTime);
        project.setVersion(version);
        project.setLastModifiedUser(lastModifiedBy);
        project.setDescription(description);

        projects.add(project);
      } while (rs.next());

      return projects;
    }
Exemple #16
0
  public void removeProjectProxyUser(Project project, String proxyName, User modifier)
      throws ProjectManagerException {
    logger.info(
        "User "
            + modifier.getUserId()
            + " removing proxy user "
            + proxyName
            + " from project "
            + project.getName());
    project.removeProxyUser(proxyName);

    projectLoader.postEvent(
        project,
        EventType.PROXY_USER,
        modifier.getUserId(),
        "Proxy user " + proxyName + " has been removed form the project.");
    updateProjectSetting(project);
  }
Exemple #17
0
  public void addProjectProxyUser(Project project, String proxyName, User modifier)
      throws ProjectManagerException {
    logger.info(
        "User "
            + modifier.getUserId()
            + " adding proxy user "
            + proxyName
            + " to project "
            + project.getName());
    project.addProxyUser(proxyName);

    projectLoader.postEvent(
        project,
        EventType.PROXY_USER,
        modifier.getUserId(),
        "Proxy user " + proxyName + " is added to project.");
    updateProjectSetting(project);
  }
Exemple #18
0
  @Override
  public void updateDescription(Project project, String description, String user)
      throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    final String UPDATE_PROJECT_DESCRIPTION =
        "UPDATE projects SET description=?,modified_time=?,last_modified_by=? WHERE id=?";
    long updateTime = System.currentTimeMillis();
    try {
      runner.update(UPDATE_PROJECT_DESCRIPTION, description, updateTime, user, project.getId());
      project.setDescription(description);
      project.setLastModifiedTimestamp(updateTime);
      project.setLastModifiedUser(user);
    } catch (SQLException e) {
      logger.error(e);
      throw new ProjectManagerException(
          "Error marking project " + project.getName() + " as inactive", e);
    }
  }
Exemple #19
0
  @Override
  public void changeProjectVersion(Project project, int version, String user)
      throws ProjectManagerException {
    long timestamp = System.currentTimeMillis();
    QueryRunner runner = createQueryRunner();
    try {
      final String UPDATE_PROJECT_VERSION =
          "UPDATE projects SET version=?,modified_time=?,last_modified_by=? WHERE id=?";

      runner.update(UPDATE_PROJECT_VERSION, version, timestamp, user, project.getId());
      project.setVersion(version);
      project.setLastModifiedTimestamp(timestamp);
      project.setLastModifiedUser(user);
    } catch (SQLException e) {
      logger.error(e);
      throw new ProjectManagerException(
          "Error updating switching project version " + project.getName(), e);
    }
  }
Exemple #20
0
  private List<Triple<String, Boolean, Permission>> fetchPermissionsForProject(
      Connection connection, Project project) throws ProjectManagerException {
    ProjectPermissionsResultHandler permHander = new ProjectPermissionsResultHandler();

    QueryRunner runner = new QueryRunner();
    List<Triple<String, Boolean, Permission>> permissions = null;
    try {
      permissions =
          runner.query(
              connection,
              ProjectPermissionsResultHandler.SELECT_PROJECT_PERMISSION,
              permHander,
              project.getId());
    } catch (SQLException e) {
      throw new ProjectManagerException(
          "Query for permissions for " + project.getName() + " failed.", e);
    }

    return permissions;
  }
Exemple #21
0
  public Project createProject(String projectName, String description, User creator)
      throws ProjectManagerException {
    if (projectName == null || projectName.trim().isEmpty()) {
      throw new ProjectManagerException("Project name cannot be empty.");
    } else if (description == null || description.trim().isEmpty()) {
      throw new ProjectManagerException("Description cannot be empty.");
    } else if (creator == null) {
      throw new ProjectManagerException("Valid creator user must be set.");
    } else if (!projectName.matches("[a-zA-Z][a-zA-Z_0-9|-]*")) {
      throw new ProjectManagerException(
          "Project names must start with a letter, followed by any number of letters, digits, '-' or '_'.");
    }

    if (projectsByName.contains(projectName)) {
      throw new ProjectManagerException("Project already exists.");
    }

    logger.info("Trying to create " + projectName + " by user " + creator.getUserId());
    Project newProject = projectLoader.createNewProject(projectName, description, creator);
    projectsByName.put(newProject.getName(), newProject);
    projectsById.put(newProject.getId(), newProject);

    if (creatorDefaultPermissions) {
      // Add permission to project
      projectLoader.updatePermission(
          newProject, creator.getUserId(), new Permission(Permission.Type.ADMIN), false);

      // Add proxy user
      newProject.getProxyUsers().add(creator.getUserId());
      try {
        updateProjectSetting(newProject);
      } catch (ProjectManagerException e) {
        e.printStackTrace();
        throw e;
      }
    }

    projectLoader.postEvent(newProject, EventType.CREATED, creator.getUserId(), null);

    return newProject;
  }
Exemple #22
0
  public List<Project> getUserProjectsByRegex(User user, String regexPattern) {
    List<Project> array = new ArrayList<Project>();
    Pattern pattern;
    try {
      pattern = Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE);
    } catch (PatternSyntaxException e) {
      logger.error("Bad regex pattern " + regexPattern);
      return array;
    }

    for (Project project : projectsById.values()) {
      Permission perm = project.getUserPermission(user);

      if (perm != null && (perm.isPermissionSet(Type.ADMIN) || perm.isPermissionSet(Type.READ))) {
        if (pattern.matcher(project.getName()).find()) {
          array.add(project);
        }
      }
    }
    return array;
  }
Exemple #23
0
  @Override
  public void removePermission(Project project, String name, boolean isGroup)
      throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();
    final String DELETE_PROJECT_PERMISSION =
        "DELETE FROM project_permissions WHERE project_id=? AND name=? AND isGroup=?";

    try {
      runner.update(DELETE_PROJECT_PERMISSION, project.getId(), name, isGroup);
    } catch (SQLException e) {
      logger.error(e);
      throw new ProjectManagerException(
          "Error deleting project " + project.getName() + " permissions for " + name, e);
    }

    if (isGroup) {
      project.removeGroupPermission(name);
    } else {
      project.removeUserPermission(name);
    }
  }
Exemple #24
0
  private void loadAllProjectFlows(Project project) {
    try {
      List<Flow> flows = projectLoader.fetchAllProjectFlows(project);
      Map<String, Flow> flowMap = new HashMap<String, Flow>();
      for (Flow flow : flows) {
        flowMap.put(flow.getId(), flow);
      }

      project.setFlows(flowMap);
    } catch (ProjectManagerException e) {
      throw new RuntimeException("Could not load projects flows from store.", e);
    }
  }
Exemple #25
0
  @Override
  public Flow fetchFlow(Project project, String flowId) throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();
    ProjectFlowsResultHandler handler = new ProjectFlowsResultHandler();

    try {
      List<Flow> flows =
          runner.query(
              ProjectFlowsResultHandler.SELECT_PROJECT_FLOW,
              handler,
              project.getId(),
              project.getVersion(),
              flowId);
      if (flows.isEmpty()) {
        return null;
      } else {
        return flows.get(0);
      }
    } catch (SQLException e) {
      throw new ProjectManagerException("Error fetching flow " + flowId, e);
    }
  }
Exemple #26
0
  private void updateProjectProperty(
      Connection connection, Project project, String name, Props props)
      throws ProjectManagerException, IOException {
    QueryRunner runner = new QueryRunner();
    final String UPDATE_PROPERTIES =
        "UPDATE project_properties SET property=? WHERE project_id=? AND version=? AND name=?";

    String propertyJSON = PropsUtils.toJSONString(props, true);
    byte[] data = propertyJSON.getBytes("UTF-8");
    logger.info("UTF-8 size:" + data.length);
    if (defaultEncodingType == EncodingType.GZIP) {
      data = GZIPUtils.gzipBytes(data);
    }

    try {
      runner.update(
          connection, UPDATE_PROPERTIES, data, project.getId(), project.getVersion(), name);
      connection.commit();
    } catch (SQLException e) {
      throw new ProjectManagerException(
          "Error updating property " + project.getName() + " version " + project.getVersion(), e);
    }
  }
Exemple #27
0
  @Override
  public Props fetchProjectProperty(Project project, String propsName)
      throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    ProjectPropertiesResultsHandler handler = new ProjectPropertiesResultsHandler();
    try {
      List<Pair<String, Props>> properties =
          runner.query(
              ProjectPropertiesResultsHandler.SELECT_PROJECT_PROPERTY,
              handler,
              project.getId(),
              project.getVersion(),
              propsName);

      if (properties == null || properties.isEmpty()) {
        return null;
      }

      return properties.get(0).getSecond();
    } catch (SQLException e) {
      throw new ProjectManagerException("Error fetching property " + propsName, e);
    }
  }
Exemple #28
0
  private void uploadProjectProperty(
      Connection connection, Project project, String name, Props props)
      throws ProjectManagerException, IOException {
    QueryRunner runner = new QueryRunner();
    final String INSERT_PROPERTIES =
        "INSERT INTO project_properties (project_id, version, name, modified_time, encoding_type, property) values (?,?,?,?,?,?)";

    String propertyJSON = PropsUtils.toJSONString(props, true);
    byte[] data = propertyJSON.getBytes("UTF-8");
    logger.info("UTF-8 size:" + data.length);
    if (defaultEncodingType == EncodingType.GZIP) {
      data = GZIPUtils.gzipBytes(data);
    }

    try {
      runner.update(
          connection,
          INSERT_PROPERTIES,
          project.getId(),
          project.getVersion(),
          name,
          System.currentTimeMillis(),
          defaultEncodingType.getNumVal(),
          data);
      connection.commit();
    } catch (SQLException e) {
      throw new ProjectManagerException(
          "Error uploading project properties "
              + name
              + " into "
              + project.getName()
              + " version "
              + project.getVersion(),
          e);
    }
  }
Exemple #29
0
  @Override
  public List<Flow> fetchAllProjectFlows(Project project) throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();
    ProjectFlowsResultHandler handler = new ProjectFlowsResultHandler();

    List<Flow> flows = null;
    try {
      flows =
          runner.query(
              ProjectFlowsResultHandler.SELECT_ALL_PROJECT_FLOWS,
              handler,
              project.getId(),
              project.getVersion());
    } catch (SQLException e) {
      throw new ProjectManagerException(
          "Error fetching flows from project "
              + project.getName()
              + " version "
              + project.getVersion(),
          e);
    }

    return flows;
  }
  private void prepareProject(Project project, File directory)
      throws ProjectManagerException, IOException {
    DirectoryFlowLoader loader = new DirectoryFlowLoader(new Props(), logger);
    loader.loadProjectFlow(project, directory);
    if (!loader.getErrors().isEmpty()) {
      for (String error : loader.getErrors()) {
        System.out.println(error);
      }

      throw new RuntimeException("Errors found in setup");
    }

    flowMap = loader.getFlowMap();
    project.setFlows(flowMap);
    FileUtils.copyDirectory(directory, workingDir);
  }