예제 #1
0
  @Override
  public void generate(Model model, MolgenisOptions options) throws Exception {
    if (options.generate_tests) {
    } else {
      Template template =
          this.createTemplate(this.getClass().getSimpleName() + getExtension() + ".ftl");
      Map<String, Object> templateArgs = createTemplateArguments(options);

      // apply generator to each entity
      for (Entity entity : model.getEntities()) {
        // calculate package from its own package
        String packageName =
            entity.getNamespace().toLowerCase()
                + this.getClass()
                    .getPackage()
                    .toString()
                    .substring(Generator.class.getPackage().toString().length());

        File targetDir =
            new File(
                (this.getSourcePath(options).endsWith("/")
                        ? this.getSourcePath(options)
                        : this.getSourcePath(options) + "/")
                    + packageName.replace(".", "/").replace("/cpp", ""));
        try {
          File targetFile =
              new File(
                  targetDir + "/" + GeneratorHelper.getJavaName(entity.getName()) + getExtension());
          boolean created = targetDir.mkdirs();
          if (!created && !targetDir.exists()) {
            throw new IOException("could not create " + targetDir);
          }

          // logger.debug("trying to generated "+targetFile);
          templateArgs.put("entity", entity);
          templateArgs.put("model", model);
          templateArgs.put("db_driver", options.db_driver);
          templateArgs.put("template", template.getName());
          templateArgs.put(
              "file",
              targetDir.getCanonicalPath().replace("\\", "/")
                  + "/"
                  + GeneratorHelper.firstToUpper(entity.getName())
                  + getType()
                  + getExtension());
          templateArgs.put("package", packageName);

          OutputStream targetOut = new FileOutputStream(targetFile);

          template.process(
              templateArgs, new OutputStreamWriter(targetOut, Charset.forName("UTF-8")));
          targetOut.close();
          logger.info("generated " + targetFile);
        } catch (Exception e) {
          logger.error("problem generating for " + entity.getName());
          throw e;
        }
      }
    }
  }
  @Override
  @Transactional(rollbackFor = DatabaseException.class)
  public void populateDatabase() throws DatabaseException {
    if (adminPassword == null
        || userPassword == null
        || appProfileStr == null
        || dataManagerPassword == null
        || researcherPassword == null
        || adminPassword == null) {
      StringBuilder message = new StringBuilder("please configure: ");
      if (adminPassword == null) message.append("default admin.password, ");
      if (userPassword == null) message.append("default user.password, ");
      if (appProfileStr == null)
        message.append("lifelines.profile(possible values: workspace or website), ");
      if (dataManagerPassword == null) message.append("default lifelines.datamanager.password, ");
      if (researcherPassword == null) message.append("default lifelines.researcher.password ");
      message.append("in your molgenis-server.properties.");
      throw new RuntimeException(message.toString());
    }

    // FIXME create users and groups through service class
    MolgenisUser userAdmin = new MolgenisUser();
    userAdmin.setUsername(USERNAME_ADMIN);
    userAdmin.setPassword(new BCryptPasswordEncoder().encode(adminPassword));
    userAdmin.setEmail(adminEmail);
    userAdmin.setActive(true);
    userAdmin.setSuperuser(true);
    unsecuredDatabase.add(userAdmin);

    UserAuthority suAuthority = new UserAuthority();
    suAuthority.setMolgenisUser(userAdmin);
    suAuthority.setRole("ROLE_SU");
    unsecuredDatabase.add(suAuthority);

    MolgenisUser userUser = new MolgenisUser();
    userUser.setUsername(USERNAME_USER);
    userUser.setPassword(new BCryptPasswordEncoder().encode(userPassword));
    userUser.setEmail(userEmail);
    userUser.setActive(true);
    userUser.setSuperuser(false);
    unsecuredDatabase.add(userUser);

    MolgenisGroup allUsersGroup = new MolgenisGroup();
    allUsersGroup.setName("All Users");
    unsecuredDatabase.add(allUsersGroup);

    MolgenisGroupMember userAllUsersMember = new MolgenisGroupMember();
    userAllUsersMember.setMolgenisGroup(allUsersGroup);
    userAllUsersMember.setMolgenisUser(userUser);
    unsecuredDatabase.add(userAllUsersMember);

    Vector<Entity> entities = unsecuredDatabase.getMetaData().getEntities();
    for (Entity entity : entities) {
      GroupAuthority entityAuthority = new GroupAuthority();
      entityAuthority.setMolgenisGroup(allUsersGroup);
      entityAuthority.setRole(
          SecurityUtils.AUTHORITY_ENTITY_READ_PREFIX + entity.getName().toUpperCase());
      unsecuredDatabase.add(entityAuthority);
    }

    // lifelines database populator
    LifeLinesAppProfile appProfile = LifeLinesAppProfile.valueOf(appProfileStr.toUpperCase());

    Map<String, String> runtimePropertyMap = new HashMap<String, String>();
    runtimePropertyMap.put(KEY_APP_NAME, "LifeLines");
    runtimePropertyMap.put(KEY_APP_HREF_LOGO, "/img/lifelines_letterbox_270x100.png");
    runtimePropertyMap.put(KEY_APP_HREF_CSS, "lifelines.css");
    runtimePropertyMap.put(AccountService.KEY_PLUGIN_AUTH_ACTIVATIONMODE, "user");
    if (appProfile == LifeLinesAppProfile.WEBSITE) {
      runtimePropertyMap.put(
          SearchSecurityHandlerInterceptor.KEY_ACTION_ALLOW_ANONYMOUS_SEARCH, "true");
    }
    runtimePropertyMap.put(
        HomeController.KEY_APP_HOME_HTML,
        "<div class=\"container-fluid\">"
            + "<div class=\"row-fluid\">"
            + "<div class=\"span6\">"
            + "<h3>Welcome at the LifeLines Data Catalogue!</h3>"
            + "<p>The LifeLines Data Catalogue provides an overview of all the data collected in LifeLines.</p>"
            + "<p>When you click 'catalogue' you can browse all available data items from questionnaires,  measurements and (blood and urine) sample analysis. Also, you can make a selection of data  items that you will need for your research, and download the list.</p>"
            + "<p>If you want to save your selection and apply for LifeLines data, you need to  register first. You can register by clicking the 'login' button on top. After you  have registered, you will receive a confirmation email. Subsequently, you are able  to download your selection or submit the selection together with you proposal.</p>"
            + "<p>The catalogue will regularly be updated with new collected data items.  For questions regarding the catalogue or submission of your proposal, please contact the  LifeLines Research Office  <a href=\"mailto:[email protected]\">[email protected]</a></p>"
            + "<p>The catalogue is working in the newest browsers. <u>If you are experiencing any problems  please switch to a modern browser (IE9+, Chrome, Firefox, Safari).</u></p>"
            + "</div>"
            + "<div class=\"span6\">"
            + "<img src=\"/img/lifelines_family.png\" alt=\"LifeLines family\">"
            + "</div>"
            + "</div>"
            + "</div>");

    for (Entry<String, String> entry : runtimePropertyMap.entrySet()) {
      RuntimeProperty runtimeProperty = new RuntimeProperty();
      String propertyKey = entry.getKey();
      runtimeProperty.setIdentifier(RuntimeProperty.class.getSimpleName() + '_' + propertyKey);
      runtimeProperty.setName(propertyKey);
      runtimeProperty.setValue(entry.getValue());
      unsecuredDatabase.add(runtimeProperty);
    }

    // FIXME create users and groups through service class
    MolgenisUser datamanagerUser = new MolgenisUser();
    datamanagerUser.setUsername(USERNAME_DATAMANAGER);
    datamanagerUser.setPassword(new BCryptPasswordEncoder().encode(dataManagerPassword));
    datamanagerUser.setEmail(dataManagerEmail);
    datamanagerUser.setActive(true);
    datamanagerUser.setSuperuser(false);
    unsecuredDatabase.add(datamanagerUser);

    MolgenisGroupMember datamanagerUserAllUsersMember = new MolgenisGroupMember();
    datamanagerUserAllUsersMember.setMolgenisUser(datamanagerUser);
    datamanagerUserAllUsersMember.setMolgenisGroup(allUsersGroup);
    unsecuredDatabase.add(datamanagerUserAllUsersMember);

    MolgenisGroup datamanagerGroup = new MolgenisGroup();
    datamanagerGroup.setName(GROUP_DATAMANAGERS);
    unsecuredDatabase.add(datamanagerGroup);

    MolgenisGroupMember datamanagerUserDataManagerGroupMember = new MolgenisGroupMember();
    datamanagerUserDataManagerGroupMember.setMolgenisUser(datamanagerUser);
    datamanagerUserDataManagerGroupMember.setMolgenisGroup(datamanagerGroup);
    unsecuredDatabase.add(datamanagerUserDataManagerGroupMember);

    GroupAuthority allUsersHomeAuthority = new GroupAuthority();
    allUsersHomeAuthority.setMolgenisGroup(allUsersGroup);
    allUsersHomeAuthority.setRole(
        SecurityUtils.AUTHORITY_PLUGIN_WRITE_PREFIX + HomeController.ID.toUpperCase());
    unsecuredDatabase.add(allUsersHomeAuthority);

    GroupAuthority allUsersProtocolViewerAuthority = new GroupAuthority();
    allUsersProtocolViewerAuthority.setMolgenisGroup(allUsersGroup);
    allUsersProtocolViewerAuthority.setRole(
        SecurityUtils.AUTHORITY_PLUGIN_WRITE_PREFIX + ProtocolViewerController.ID.toUpperCase());
    unsecuredDatabase.add(allUsersProtocolViewerAuthority);

    GroupAuthority allUsersAccountAuthority = new GroupAuthority();
    allUsersAccountAuthority.setMolgenisGroup(allUsersGroup);
    allUsersAccountAuthority.setRole(
        SecurityUtils.AUTHORITY_PLUGIN_WRITE_PREFIX + UserAccountController.ID.toUpperCase());
    unsecuredDatabase.add(allUsersAccountAuthority);

    GroupAuthority datamanagerStudyManagerAuthority = new GroupAuthority();
    datamanagerStudyManagerAuthority.setMolgenisGroup(allUsersGroup);
    datamanagerStudyManagerAuthority.setRole(
        SecurityUtils.AUTHORITY_PLUGIN_WRITE_PREFIX + StudyManagerController.ID.toUpperCase());
    unsecuredDatabase.add(datamanagerStudyManagerAuthority);

    GroupAuthority datamanagerCatalogManagerAuthority = new GroupAuthority();
    datamanagerCatalogManagerAuthority.setMolgenisGroup(allUsersGroup);
    datamanagerCatalogManagerAuthority.setRole(
        SecurityUtils.AUTHORITY_PLUGIN_WRITE_PREFIX + CatalogManagerController.ID.toUpperCase());
    unsecuredDatabase.add(datamanagerCatalogManagerAuthority);

    if (appProfile == LifeLinesAppProfile.WORKSPACE) {
      MolgenisUser researcherUser = new MolgenisUser();
      researcherUser.setUsername(USERNAME_RESEARCHER);
      researcherUser.setPassword(new BCryptPasswordEncoder().encode(researcherPassword));
      researcherUser.setEmail(researcherEmail);
      researcherUser.setActive(true);
      researcherUser.setSuperuser(false);
      unsecuredDatabase.add(researcherUser);

      MolgenisGroupMember researcherUserAllUsersMember = new MolgenisGroupMember();
      researcherUserAllUsersMember.setMolgenisUser(researcherUser);
      researcherUserAllUsersMember.setMolgenisGroup(allUsersGroup);
      unsecuredDatabase.add(researcherUserAllUsersMember);

      MolgenisGroup researcherGroup = new MolgenisGroup();
      researcherGroup.setName(GROUP_RESEARCHERS);
      unsecuredDatabase.add(researcherGroup);

      MolgenisGroupMember researcherUserDataManagerGroupMember = new MolgenisGroupMember();
      researcherUserDataManagerGroupMember.setMolgenisUser(researcherUser);
      researcherUserDataManagerGroupMember.setMolgenisGroup(researcherGroup);
      unsecuredDatabase.add(researcherUserDataManagerGroupMember);

      GroupAuthority allUsersDataExplorerAuthority = new GroupAuthority();
      allUsersDataExplorerAuthority.setMolgenisGroup(allUsersGroup);
      allUsersDataExplorerAuthority.setRole(
          SecurityUtils.AUTHORITY_PLUGIN_WRITE_PREFIX + DataExplorerController.ID.toUpperCase());
      unsecuredDatabase.add(allUsersDataExplorerAuthority);
    }
  }