Package org.platformlayer.ids

Examples of org.platformlayer.ids.ManagedItemId


      }
    }

    ProjectId project = getProjectId(auth);
    PlatformLayerKey itemKey = new PlatformLayerKey(null, project, modelClass.getServiceType(),
        modelClass.getItemType(), new ManagedItemId(id));
    item.setKey(itemKey);

    item.state = ManagedItemState.CREATION_REQUESTED;

    final OpsContext opsContext = buildTemporaryOpsContext(modelClass.getServiceType(), auth);

    T created = OpsContext.runInContext(opsContext, new CheckedCallable<T, Exception>() {
      @Override
      public T call() throws Exception {
        PlatformLayerKey itemKey = item.getKey();

        T existing;

        SecretProvider secretProvider = SecretProvider.from(auth);

        if (uniqueTagKey != null) {
          boolean fetchTags = true;
          Tag uniqueTag = null;
          for (Tag tag : item.getTags()) {
            if (Objects.equal(tag.getKey(), uniqueTagKey)) {
              uniqueTag = tag;
            }
          }
          if (uniqueTag == null) {
            throw new IllegalArgumentException("Could not find unique tag");
          }
          Filter filter = TagFilter.byTag(uniqueTag);
          filter = StateFilter.excludeDeleted(filter);

          existing = null;
          List<T> existingList = repository.findAll(modelClass, itemKey.getProject(), fetchTags,
              secretProvider, filter);
          if (!existingList.isEmpty()) {
            if (existingList.size() != 1) {
              throw new IllegalArgumentException("Found multiple items with unique tag");
            }
            existing = existingList.get(0);
          }

          if (existing == null) {
            itemKey = findUniqueId(item, itemKey, secretProvider);
          }
        } else {
          if (generateUniqueName) {
            itemKey = findUniqueId(item, itemKey, secretProvider);
          }

          try {
            boolean fetchTags = true;
            existing = Casts.checkedCast(repository.getManagedItem(itemKey, fetchTags, secretProvider),
                javaClass);
          } catch (RepositoryException e) {
            throw new OpsException("Error fetching item from database", e);
          }
        }

        if (!canExist && existing != null) {
          throw new OpsException("Item already exists");
        }

        serviceProvider.beforeCreateItem(item);

        ProjectId project = getProjectId(auth);
        T newItem;
        try {
          if (existing == null) {
            newItem = repository.createManagedItem(project, item);
          } else {
            item.secret = existing.secret;
            item.setKey(existing.getKey());

            newItem = repository.updateManagedItem(project, item);

            TagChanges tagChanges = new TagChanges();
            for (Tag tag : item.getTags()) {
              if (newItem.getTags().hasTag(tag)) {
                continue;
              }

              boolean uniqueTagKey = false;
              if (tag.getKey().equals(Tag.PARENT.getKey())) {
                uniqueTagKey = true;
              }

              tagChanges.addTags.add(tag);

              if (uniqueTagKey) {
                for (Tag oldTag : newItem.getTags().findTags(tag.getKey())) {
                  tagChanges.removeTags.add(oldTag);
                }
              }
            }

            if (!tagChanges.isEmpty()) {
              repository.changeTags(modelClass, project, newItem.getKey().getItemId(), tagChanges, null);
            }
          }
        } catch (RepositoryException e) {
          throw new OpsException("Error writing object to database", e);
        }

        itemKey = newItem.getKey();
        JobData jobKey = changeQueue.notifyChange(auth, itemKey, ManagedItemState.CREATION_REQUESTED);
        // returnJobKey(jobKey);

        return newItem;
      }

      private <T extends ItemBase> PlatformLayerKey findUniqueId(final T item, final PlatformLayerKey itemKey,
          SecretProvider secretProvider) throws RepositoryException {
        int sequence = 0;
        while (true) {
          String tryId = item.getId();
          if (sequence != 0) {
            tryId += sequence;
          }
          final PlatformLayerKey tryKey = itemKey.withId(new ManagedItemId(tryId));
          boolean fetchTags = false;
          ItemBase found = repository.getManagedItem(tryKey, fetchTags, secretProvider);
          if (found == null) {
            item.setKey(tryKey);
            return tryKey;
View Full Code Here


    // Class<T> javaClass = modelClass.getJavaClass();

    ProjectId project = getProjectId(auth);

    PlatformLayerKey modelKey = new PlatformLayerKey(null, project, modelClass.getServiceType(),
        modelClass.getItemType(), new ManagedItemId(id));

    boolean fetchTags = true;
    T managedItem;
    try {
      managedItem = Casts.checkedCast(repository.getManagedItem(modelKey, fetchTags, SecretProvider.from(auth)),
View Full Code Here

    return executions;
  }

  @Path("{jobId}")
  public JobResource getJob(@PathParam("jobId") String jobId) throws OpsException {
    PlatformLayerKey jobKey = JobData.buildKey(getProject(), new ManagedItemId(jobId));

    JobData record = jobRegistry.getJob(jobKey);
    if (record == null) {
      raiseNotFound();
    }
View Full Code Here

  }

  private void checkItemKey(ItemBase item) throws OpsException {
    PlatformLayerKey key = item.getKey();

    ManagedItemId itemId = getItemId();
    ServiceType serviceType = getServiceType();
    ItemType itemType = getItemType();
    ProjectId project = getProject();

    if (key != null) {
View Full Code Here

TOP

Related Classes of org.platformlayer.ids.ManagedItemId

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.