/**
   * Check if the user is in the period
   *
   * @param user the user
   * @param runId the run id
   * @param periodId the period id
   * @return whether the user is in the period
   */
  private boolean isUserInPeriod(User user, Long runId, Long periodId) {
    boolean result = false;

    if (user != null && runId != null && periodId != null) {
      try {
        // get the run
        Run run = runService.retrieveById(runId);

        // get the period the student is in for the run
        Group periodOfStudent = run.getPeriodOfStudent(user);

        if (periodOfStudent != null) {
          // get the period id
          Long tempPeriodId = periodOfStudent.getId();

          // check if the period id matches the one we are searching for
          if (periodId.equals(tempPeriodId)) {
            // the period id matches so the user is in the period
            result = true;
          }
        }
      } catch (ObjectNotFoundException e) {
      }
    }

    return result;
  }
  /**
   * Creates a run based on input parameters provided.
   *
   * @param runParameters
   * @return The run created.
   * @throws CurnitNotFoundException
   */
  @Transactional(rollbackFor = {HttpStatusCodeException.class})
  public Run createRun(RunParameters runParameters) throws ObjectNotFoundException {
    Project project = runParameters.getProject();
    // Project projectCopy = projectService.copyProject(project);
    Run run = new RunImpl();
    run.setEndtime(null);
    run.setStarttime(Calendar.getInstance().getTime());
    run.setRuncode(generateUniqueRunCode());
    run.setOwners(runParameters.getOwners());
    run.setMaxWorkgroupSize(runParameters.getMaxWorkgroupSize());
    run.setProject(project);

    // use the project name for the run name
    run.setName("" + runParameters.getProject().getName());

    Calendar reminderCal = Calendar.getInstance();
    reminderCal.add(Calendar.DATE, 30);
    run.setArchiveReminderTime(reminderCal.getTime());
    if (!(run.getProject() instanceof ExternalProject)) {
      if (run.getProject().getProjectType() != ProjectType.ROLOO
          && run.getProject().getProjectType() != ProjectType.LD) {
        run.setSdsOffering(generateSdsOfferingFromParameters(runParameters));
      }
    }
    Set<String> periodNames = runParameters.getPeriodNames();
    if (periodNames != null) {
      Set<Group> periods = new TreeSet<Group>();
      for (String periodName : runParameters.getPeriodNames()) {
        Group group = new PersistentGroup();
        group.setName(periodName);
        this.groupDao.save(group);
        periods.add(group);
      }
      run.setPeriods(periods);
    }
    run.setPostLevel(runParameters.getPostLevel());

    /* if this is an LD project take snapshot of project for run and set versionId
    if(run.getProject().getProjectType()==ProjectType.LD){
    	String requester = run.getOwners().iterator().next().getUserDetails().getUsername();
    	String versionId = this.projectService.takeSnapshot(run.getProject(), requester, "For Run " + run.getName());
    	if(versionId==null || versionId.equals("failed")){
    		throw new ObjectNotFoundException("Snapshot of project failed when creating the run.", RunImpl.class);
    	} else{
    		run.setVersionId(versionId);
    	}
    }
    */

    this.runDao.save(run);
    this.aclService.addPermission(run, BasePermission.ADMINISTRATION);
    return run;
  }
 @Transactional()
 public void addPeriodToRun(Long runId, String name) {
   try {
     Run run = this.retrieveById(runId);
     Set<Group> periods = run.getPeriods();
     Group group = new PersistentGroup();
     group.setName(name);
     this.groupDao.save(group);
     periods.add(group);
     this.runDao.save(run);
   } catch (ObjectNotFoundException e) {
     e.printStackTrace();
   }
 }