private GovernanceArtifact findMatchingArtifact(RBGGovernanceArtifact ga) {
   GovernanceArtifact ret = null;
   String matcher = getArtifactPath(ga);
   try {
     if (registryFactory.getRemoteRegistryInstance().resourceExists(matcher)) {
       ret =
           GovernanceUtils.retrieveGovernanceArtifactByPath(
               registryFactory.getRemoteRegistryInstance(), matcher);
     }
   } catch (RegistryException e) {
     log.error(e.getMessage());
   }
   return ret;
 }
  /**
   * Artifact is either retrieved or prepared for later addition to registry
   *
   * @param ga
   * @return
   */
  private GovernanceArtifact addArtifact(RBGGovernanceArtifact ga) {
    boolean checkChanged = false;
    boolean isNewer = false;
    GenericArtifactManager gam = registryFactory.getManagerForArtifactType(ga.getRegistryType());
    GenericArtifact fa = (GenericArtifact) findMatchingArtifact(ga);
    log.info("working on: " + ga.getName());
    try {
      if (fa != null) {
        isNewer = isVersionNewer(ga.getVersion(), fa.getAttribute("details_version"));
        checkChanged = updateAttributes(ga, fa);
      } else {
        fa = gam.newGovernanceArtifact(new QName(ga.getName())); // artifactname
        fa.setAttribute("details_version", ga.getVersion());
        updateAttributes(ga, fa);
        gam.addGenericArtifact(fa);
        handleLifeCycle(ga, fa);
      }
      if (isNewer) {
        if (checkChanged) {
          gam.updateGenericArtifact(fa);
          log.info("updated: " + ga.getName());
        }
        handleLifeCycle(ga, fa);
      }
    } catch (GovernanceException e) {
      log.error(e.getMessage());
    } catch (RegistryException e) {
      log.error(e.getMessage());
    }

    return fa;
  }
  private void processModelContainer(ModelContainer mc) {
    Map<RBGGovernanceArtifact, GovernanceArtifact> rbgToGen =
        new HashMap<RBGGovernanceArtifact, GovernanceArtifact>();
    Set<RBGGovernanceArtifact> toBeUpated = new HashSet<RBGGovernanceArtifact>();

    // create Artifacts: apps, modules, appservices
    for (Application app : mc.getApplications()) {
      GovernanceArtifact ga = addArtifact(app);
      rbgToGen.put(app, ga);
    }
    for (Module module : mc.getModules()) {
      GovernanceArtifact ga = addArtifact(module);
      rbgToGen.put(module, ga);
    }
    for (ApplicationService as : mc.getApplicationServices()) {
      if (as instanceof HostProgram) {
        HostProgram hp = (HostProgram) as;
        GovernanceArtifact ga = addArtifact(hp);
        rbgToGen.put(as, ga);
      }
    }
    for (Table table : mc.getTables()) {
      GovernanceArtifact ga = addArtifact(table);
      rbgToGen.put(table, ga);
    }

    for (Infrastructure infra : mc.getInfrastructure()) {
      GovernanceArtifact ga = addArtifact(infra);
      rbgToGen.put(infra, ga);
    }

    // create Links
    for (UseRelation ur : mc.getUseRelations()) {
      GovernanceArtifact ga = rbgToGen.get(ur.getSource());
      boolean needsUpdate = createLink(ur, ga);
      if (needsUpdate) {
        toBeUpated.add(ur.getSource());
      } // CHECK FOR CHANGED VRSION!!
    }
    for (CallRelation cr : mc.getCallRelations()) {
      GovernanceArtifact ga = rbgToGen.get(cr.getSource());
      boolean needsUpdate = createLink(cr, ga);
      if (needsUpdate) {
        toBeUpated.add(cr.getSource());
      }
    }

    // update those, which really need it...
    List<RBGGovernanceArtifact> upds = new ArrayList<RBGGovernanceArtifact>(toBeUpated);
    for (RBGGovernanceArtifact ra : upds) {
      GenericArtifact ga = (GenericArtifact) rbgToGen.get(ra);
      GenericArtifactManager gam = registryFactory.getManagerForArtifactType(ra.getRegistryType());
      try {
        gam.updateGenericArtifact(ga);
      } catch (GovernanceException e) {
        log.error("link creation problem addGenericArtifact " + e.getMessage());
      }
    }
  }
  @Override
  public void process(Exchange ex) throws Exception {
    registryFactory.checkAlive();

    ModelContainer mc = (ModelContainer) ex.getIn().getBody();
    processModelContainer(mc);

    log.info("ModelContainer was processed.");
  }
 private String getArtifactPath(RBGGovernanceArtifact ga) {
   GovernanceArtifactConfiguration cf =
       registryFactory.getConfigForArtifactType(ga.getRegistryType());
   String path = cf.getPathExpression();
   path = path.replace("@{" + cf.getArtifactNameAttribute() + "}", ga.getName());
   // TODO: extract hardcoded details_version attribute-name
   path = path.replace("@{details_version}", ga.getVersion());
   if (ga.getApplCode() != null) {
     path = path.replace("@{details_application}", ga.getApplCode());
   }
   if (ga instanceof Infrastructure && ((Infrastructure) ga).getComponentnumber() != null) {
     path = path.replace("@{details_componentnumber}", ((Infrastructure) ga).getComponentnumber());
   }
   return path;
 }
  /**
   * set lifecycle according to handed over state TODO: rework this, when changing the lifecycle
   * because this depends on the transition NAMES!
   *
   * @param ga
   * @param fa
   * @throws RegistryException
   */
  private void handleLifeCycle(RBGGovernanceArtifact ga, GenericArtifact fa)
      throws RegistryException {
    String path = getArtifactPath(ga);

    String lName = ga.getLifecycleName();
    String oldLName = fa.getLifecycleName();

    String lState = null;
    if (ga.getLifecycleState() != null) {
      lState = ga.getLifecycleState().toString();
    }
    String oldLState = fa.getLifecycleState();

    boolean freshLifecycle = false;
    // check, if lifecycleNames are different
    // attach lifecycle, if different
    if ((lName != null && !lName.equals(oldLName) || (lName == null && oldLName != null))) {
      // this puts us to DESIGN (initial) state
      fa.attachLifecycle(ga.getLifecycleName());
      freshLifecycle = true;
    }

    // state is possibly different at this point
    if (lName != null) {
      // if state is null and lifecyclename is set, we ignore that!
      if (lState != null && !lState.equals(oldLState)) {
        // we have to reset lifecycle, to start from a clean state.
        if (!freshLifecycle) {
          fa.attachLifecycle(null);
          fa.attachLifecycle(lName);
        }
        try {
          CustomLifecyclesChecklistAdminServiceStub clc =
              registryFactory.getCustomLifecyclesChecklistAdminServiceStub();
          if (LifecycleState.PRODUCTION.equals(ga.getLifecycleState())) {
            // {2.item=false, 0.item=false, 1.item=true} => it looks like this in debugger
            // EmbeddedRegistry.invokeAspect()!
            // these are representing the check boxes in the UI
            String[] params = {"false", "true", "false"};
            clc.invokeAspect(
                RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH + path,
                ga.getLifecycleName(),
                "zum Produktionsstatus",
                params);
          } else if (LifecycleState.DEVELOPMENT.equals(ga.getLifecycleState())) {
            String[] params = {"true", "false", "false"};
            clc.invokeAspect(
                RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH + path,
                ga.getLifecycleName(),
                "zur Entwicklung",
                params);
          }

        } catch (RemoteException e) {
          log.error(e.getMessage());
        } catch (CustomLifecyclesChecklistAdminServiceExceptionException e) {
          log.error(e.getMessage());
        }
      }
    }
  }