private ArrayList<StudyEventDefinitionBean> initDefinitions(StudyBean site) {
   ArrayList<StudyEventDefinitionBean> seds = new ArrayList<StudyEventDefinitionBean>();
   StudyEventDefinitionDAO sedDao = new StudyEventDefinitionDAO(sm.getDataSource());
   EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(sm.getDataSource());
   CRFVersionDAO cvdao = new CRFVersionDAO(sm.getDataSource());
   CRFDAO cdao = new CRFDAO(sm.getDataSource());
   StudyBean parentStudy =
       (StudyBean) new StudyDAO(sm.getDataSource()).findByPK(site.getParentStudyId());
   seds = sedDao.findAllByStudy(parentStudy);
   int start = 0;
   for (StudyEventDefinitionBean sed : seds) {
     int defId = sed.getId();
     ArrayList<EventDefinitionCRFBean> edcs =
         (ArrayList<EventDefinitionCRFBean>)
             edcdao.findAllByDefinitionAndSiteIdAndParentStudyId(
                 defId, site.getId(), parentStudy.getId());
     ArrayList<EventDefinitionCRFBean> defCrfs = new ArrayList<EventDefinitionCRFBean>();
     // sed.setCrfNum(edcs.size());
     for (EventDefinitionCRFBean edcBean : edcs) {
       int edcStatusId = edcBean.getStatus().getId();
       CRFBean crf = (CRFBean) cdao.findByPK(edcBean.getCrfId());
       int crfStatusId = crf.getStatusId();
       if (edcStatusId == 5 || edcStatusId == 7 || crfStatusId == 5 || crfStatusId == 7) {
       } else {
         ArrayList<CRFVersionBean> versions =
             (ArrayList<CRFVersionBean>) cvdao.findAllActiveByCRF(edcBean.getCrfId());
         edcBean.setVersions(versions);
         edcBean.setCrfName(crf.getName());
         CRFVersionBean defaultVersion =
             (CRFVersionBean) cvdao.findByPK(edcBean.getDefaultVersionId());
         edcBean.setDefaultVersionName(defaultVersion.getName());
         String sversionIds = edcBean.getSelectedVersionIds();
         ArrayList<Integer> idList = new ArrayList<Integer>();
         if (sversionIds.length() > 0) {
           String[] ids = sversionIds.split("\\,");
           for (String id : ids) {
             idList.add(Integer.valueOf(id));
           }
         }
         edcBean.setSelectedVersionIdList(idList);
         defCrfs.add(edcBean);
         ++start;
       }
     }
     logger.debug("definitionCrfs size=" + defCrfs.size() + " total size=" + edcs.size());
     sed.setCrfs(defCrfs);
     sed.setCrfNum(defCrfs.size());
   }
   return seds;
 }
  private ArrayList<StudyEventDefinitionBean> createSiteEventDefinitions(StudyBean site) {
    FormProcessor fp = new FormProcessor(request);
    ArrayList<StudyEventDefinitionBean> seds = new ArrayList<StudyEventDefinitionBean>();
    StudyBean parentStudy =
        (StudyBean) new StudyDAO(sm.getDataSource()).findByPK(site.getParentStudyId());
    seds = (ArrayList<StudyEventDefinitionBean>) session.getAttribute("definitions");
    if (seds == null || seds.size() <= 0) {
      StudyEventDefinitionDAO sedDao = new StudyEventDefinitionDAO(sm.getDataSource());
      seds = sedDao.findAllByStudy(parentStudy);
    }
    CRFVersionDAO cvdao = new CRFVersionDAO(sm.getDataSource());
    HashMap<String, Boolean> changes = new HashMap<String, Boolean>();
    for (StudyEventDefinitionBean sed : seds) {
      EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(sm.getDataSource());
      ArrayList<EventDefinitionCRFBean> edcs = sed.getCrfs();
      int start = 0;
      for (EventDefinitionCRFBean edcBean : edcs) {
        int edcStatusId = edcBean.getStatus().getId();
        if (edcStatusId == 5 || edcStatusId == 7) {
        } else {
          String order = start + "-" + edcBean.getId();
          int defaultVersionId = fp.getInt("defaultVersionId" + order);
          String requiredCRF = fp.getString("requiredCRF" + order);
          String doubleEntry = fp.getString("doubleEntry" + order);
          String electronicSignature = fp.getString("electronicSignature" + order);
          String hideCRF = fp.getString("hideCRF" + order);
          int sdvId = fp.getInt("sdvOption" + order);
          ArrayList<String> selectedVersionIdList = fp.getStringArray("versionSelection" + order);
          int selectedVersionIdListSize = selectedVersionIdList.size();
          String selectedVersionIds = "";
          if (selectedVersionIdListSize > 0) {
            for (String id : selectedVersionIdList) {
              selectedVersionIds += id + ",";
            }
            selectedVersionIds = selectedVersionIds.substring(0, selectedVersionIds.length() - 1);
          }

          boolean changed = false;
          boolean isRequired =
              !StringUtil.isBlank(requiredCRF) && "yes".equalsIgnoreCase(requiredCRF.trim())
                  ? true
                  : false;
          boolean isDouble =
              !StringUtil.isBlank(doubleEntry) && "yes".equalsIgnoreCase(doubleEntry.trim())
                  ? true
                  : false;
          boolean hasPassword =
              !StringUtil.isBlank(electronicSignature)
                      && "yes".equalsIgnoreCase(electronicSignature.trim())
                  ? true
                  : false;
          boolean isHide =
              !StringUtil.isBlank(hideCRF) && "yes".equalsIgnoreCase(hideCRF.trim()) ? true : false;
          if (edcBean.getParentId() > 0) {
            int dbDefaultVersionId = edcBean.getDefaultVersionId();
            if (defaultVersionId != dbDefaultVersionId) {
              changed = true;
              CRFVersionBean defaultVersion = (CRFVersionBean) cvdao.findByPK(defaultVersionId);
              edcBean.setDefaultVersionId(defaultVersionId);
              edcBean.setDefaultVersionName(defaultVersion.getName());
            }
            if (isRequired != edcBean.isRequiredCRF()) {
              changed = true;
              edcBean.setRequiredCRF(isRequired);
            }
            if (isDouble != edcBean.isDoubleEntry()) {
              changed = true;
              edcBean.setDoubleEntry(isDouble);
            }
            if (hasPassword != edcBean.isElectronicSignature()) {
              changed = true;
              edcBean.setElectronicSignature(hasPassword);
            }
            if (isHide != edcBean.isHideCrf()) {
              changed = true;
              edcBean.setHideCrf(isHide);
            }
            if (!StringUtil.isBlank(selectedVersionIds)
                && !selectedVersionIds.equals(edcBean.getSelectedVersionIds())) {
              changed = true;
              String[] ids = selectedVersionIds.split(",");
              ArrayList<Integer> idList = new ArrayList<Integer>();
              for (String id : ids) {
                idList.add(Integer.valueOf(id));
              }
              edcBean.setSelectedVersionIdList(idList);
              edcBean.setSelectedVersionIds(selectedVersionIds);
            }
            if (sdvId > 0 && sdvId != edcBean.getSourceDataVerification().getCode()) {
              changed = true;
              edcBean.setSourceDataVerification(SourceDataVerification.getByCode(sdvId));
            }
          } else {
            // only if definition-crf has been modified, will it be
            // saved for the site
            int defaultId = defaultVersionId > 0 ? defaultVersionId : edcBean.getDefaultVersionId();
            if (defaultId == defaultVersionId) {
              if (isRequired == edcBean.isRequiredCRF()) {
                if (isDouble == edcBean.isDoubleEntry()) {
                  if (hasPassword == edcBean.isElectronicSignature()) {
                    if (isHide == edcBean.isHideCrf()) {
                      if (selectedVersionIdListSize > 0) {
                        if (selectedVersionIdListSize == edcBean.getVersions().size()) {
                          if (sdvId > 0) {
                            if (sdvId != edcBean.getSourceDataVerification().getCode()) {
                              changed = true;
                              edcBean.setSourceDataVerification(
                                  SourceDataVerification.getByCode(sdvId));
                            }
                          }
                        } else {
                          changed = true;
                          String[] ids = selectedVersionIds.split(",");
                          ArrayList<Integer> idList = new ArrayList<Integer>();
                          for (String id : ids) {
                            idList.add(Integer.valueOf(id));
                          }
                          edcBean.setSelectedVersionIdList(idList);
                          edcBean.setSelectedVersionIds(selectedVersionIds);
                        }
                      }
                    } else {
                      changed = true;
                      edcBean.setHideCrf(isHide);
                    }
                  } else {
                    changed = true;
                    edcBean.setElectronicSignature(hasPassword);
                  }
                } else {
                  changed = true;
                  edcBean.setDoubleEntry(isDouble);
                }
              } else {
                changed = true;
                edcBean.setRequiredCRF(isRequired);
              }
            } else {
              changed = true;
              CRFVersionBean defaultVersion = (CRFVersionBean) cvdao.findByPK(defaultVersionId);
              edcBean.setDefaultVersionId(defaultVersionId);
              edcBean.setDefaultVersionName(defaultVersion.getName());
            }
          }
          changes.put(sed.getId() + "-" + edcBean.getId(), changed);
          ++start;
        }
      }
    }
    session.setAttribute("changed", changes);
    return seds;
  }