@Override
  protected ModelType finalizeElement() {
    super.finalizeElement();

    element.setVendor(XMLConstants.VENDOR_NAME);
    element.setCarnotVersion(CurrentVersion.getVersionName());

    element.setScript(XpdlFactory.eINSTANCE.createScriptType());
    element.getScript().setType("text/ecmascript");
    element.setTypeDeclarations(XpdlFactory.eINSTANCE.createTypeDeclarationsType());

    element.setCreated(new Date().toString());

    element.setAuthor(System.getProperty("user.name"));

    element.setModelOID(0);

    // add default elements
    List<ModelInitializer> modelInitializers = CollectionUtils.newArrayList();
    modelInitializers.add(new DefaultTypesInitializer());
    modelInitializers.add(new DefaultElementsInitializer());
    // allow for third party extensions
    modelInitializers.addAll(ExtensionProviderUtils.getExtensionProviders(ModelInitializer.class));

    for (ModelInitializer initializer : modelInitializers) {
      initializer.initializeModel(element);
    }

    if (null != definition) {
      definition.build(element);
    }

    return element;
  }
  /**
   * Validate if BusinessObjectQuery returns business objects which are defined in the models
   * MODEL_NAME2 and MODEL_NAME3. Note: If BusinessObjectsList is executed standalone than
   * bos.size() == expected.size(). But if it is executed after CheckFiltering2 then bos.size() >
   * expected.size() because CheckFiltering2 deployes MODEL_NAME3 as a new version. This means that
   * bos.size() == expected.size()+2 in this case.
   */
  @Test
  public void BusinessObjectsList() {
    BusinessObjectQuery query = BusinessObjectQuery.findAll();
    query.setPolicy(new BusinessObjectQuery.Policy(BusinessObjectQuery.Option.WITH_DESCRIPTION));

    BusinessObjects bos = sf.getQueryService().getAllBusinessObjects(query);

    List<String> expected =
        CollectionUtils.newArrayListFromElements(
            Arrays.asList(
                new QName(MODEL_NAME2, "Account").toString(),
                new QName(MODEL_NAME2, "Customer").toString(),
                new QName(MODEL_NAME2, "Order").toString(),
                new QName(MODEL_NAME2, "Fund").toString(),
                new QName(MODEL_NAME2, "FundGroup").toString(),
                new QName(MODEL_NAME3, "Employee").toString(),
                new QName(MODEL_NAME3, "Fund").toString()));

    List<String> removedEntries = CollectionUtils.newArrayList(expected.size());

    for (BusinessObject bo : bos) {
      String qualifiedBOId = new QName(bo.getModelId(), bo.getId()).toString();
      if (expected.remove(qualifiedBOId)) {
        removedEntries.add(qualifiedBOId);
      } else {
        Assert.assertTrue(
            "Not expected entry: " + qualifiedBOId, removedEntries.contains(qualifiedBOId));
      }
    }
    Assert.assertTrue("Missing business objects: " + expected, expected.isEmpty());
  }
  /**
   * return just model and its participants
   *
   * @return
   * @throws Exception
   */
  public List<ModelDTO> getModelParticipants() {
    List<ModelDTO> modelList = CollectionUtils.newArrayList();
    Collection<DeployedModel> models = CollectionUtils.newArrayList();
    models = ModelCache.findModelCache().getActiveModels();

    for (DeployedModel model : models) {
      ModelDTO modelDTO = DTOBuilder.build(model, ModelDTO.class);
      modelList.add(modelDTO);
      // Add all top-level Organizations
      modelDTO.children = updateTopLevelOrganizations(model);
      // Add all top-level Roles
      modelDTO.children.addAll(updateTopLevelRoles(model));
    }

    return modelList;
  }
  /** @return */
  public List<ModelDTO> getModels(Boolean allActive, boolean includePredefinedModel)
      throws Exception {
    List<ModelDTO> modelList = CollectionUtils.newArrayList();
    try {
      Collection<DeployedModel> models = CollectionUtils.newArrayList();
      if (allActive.equals(true)) {
        models = ModelCache.findModelCache().getActiveModels();
      } else {
        models = ModelCache.findModelCache().getAllModels();
      }

      for (DeployedModel model : models) {
        if (includePredefinedModel
            || !(PredefinedConstants.PREDEFINED_MODEL_ID.equals(model.getId()))) {
          ModelDTO modelDTO = DTOBuilder.build(model, ModelDTO.class);
          String modelName = I18nUtils.getModelName(model);
          List<ProcessDefinitionDTO> processDefitionDTOList = CollectionUtils.newArrayList();
          List<Data> modelData = model.getAllData();
          List<ProcessDefinition> processDefinitions = model.getAllProcessDefinitions();
          List<ActivityDTO> activityDTOList = CollectionUtils.newArrayList();
          List<DataDTO> dataDTOList = CollectionUtils.newArrayList();

          // Create DataDTO list
          for (Data data : modelData) {
            DataDTO dataDTO = DTOBuilder.build(data, DataDTO.class);
            dataDTOList.add(dataDTO);
          }

          // Create ProcessDefinitionDTO list
          for (ProcessDefinition processDefinition : processDefinitions) {
            ProcessDefinitionDTO processDefinitionDTO =
                DTOBuilder.build(processDefinition, ProcessDefinitionDTO.class);
            processDefinitionDTO.modelName = modelName;
            processDefinitionDTO.auxillary = isAuxiliaryProcess(processDefinition);
            processDefinitionDTO.name = I18nUtils.getProcessName(processDefinition);
            processDefitionDTOList.add(processDefinitionDTO);
            activityDTOList = CollectionUtils.newArrayList();
            List<Activity> activities = processDefinition.getAllActivities();
            // Create ActivityDTO list
            for (Activity activity : activities) {
              ActivityDTO activityDTO = DTOBuilder.build(activity, ActivityDTO.class);
              activityDTO.auxillary = isAuxiliaryActivity(activity);
              activityDTO.name = I18nUtils.getActivityName(activity);
              activityDTO.runtimeElementOid = activity.getRuntimeElementOID();
              activityDTOList.add(activityDTO);
            }
            processDefinitionDTO.activities = activityDTOList;
          }
          modelDTO.processDefinitions = processDefitionDTOList;
          modelDTO.data = dataDTOList;
          modelList.add(modelDTO);

          // Add all top-level Organizations
          modelDTO.allTopLevelOrganizations = updateTopLevelOrganizations(model);

          // Add all top-level Roles
          modelDTO.allTopLevelRoles = updateTopLevelRoles(model);
        }
      }
    } catch (Exception e) {
      throw e;
    }

    return modelList;
  }