use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class AbstractComponentsResource method onPost.
/**
* Creates a new component instance in the given namespace
*
* @param namespace plain namespace
* @param name the name; used as id
*/
protected ResourceCreationResult onPost(String namespace, String name) {
ResourceCreationResult res;
if (StringUtils.isEmpty(namespace) || StringUtils.isEmpty(name)) {
res = new ResourceCreationResult(Status.BAD_REQUEST);
} else {
String id = RestUtils.createXMLidAsString(name);
DefinitionsChildId tcId;
try {
tcId = this.getDefinitionsChildId(namespace, id, false);
res = this.createComponentInstance(tcId);
// in case the resource additionally supports a name attribute, we set the original name
if (res.getStatus().equals(Status.CREATED)) {
if ((tcId instanceof ServiceTemplateId) || (tcId instanceof ArtifactTemplateId) || (tcId instanceof PolicyTemplateId)) {
// these three types have an additional name (instead of a pure id)
// we store the name
IHasName resource = (IHasName) AbstractComponentsResource.getComponentInstaceResource(tcId);
resource.setName(name);
}
}
} catch (Exception e) {
AbstractComponentsResource.LOGGER.debug("Could not create id instance", e);
res = new ResourceCreationResult(Status.INTERNAL_SERVER_ERROR);
}
}
return res;
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class AbstractComponentsResource method getListOfAllIds.
/**
* Used by org.eclipse.winery.repository.repository.client and by the artifactcreationdialog.tag. Especially the
* "name" field is used there at the UI
*
* @param grouped if given, the JSON output is grouped by namespace
* @return A list of all ids of all instances of this component type. Format: <code>[({"namespace":
* "[namespace]", "id": "[id]"},)* ]</code>.
* <p>
* If grouped is set, the list will be grouped by namespace.
* <code>[{"id": "[namsepace encoded]", "test": "[namespace decoded]", "children":[{"id": "[qName]", "text":
* "[id]"}]}]</code>
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getListOfAllIds(@QueryParam("grouped") String grouped, @QueryParam("full") @ApiParam("If set, the full information of the definition's child is returned. E.g., in the case of node types, the same result as a GET on {ns}/{id] is returned. Works only in the case of grouped.") String full) {
Class<? extends DefinitionsChildId> idClass = RestUtils.getComponentIdClassForComponentContainer(this.getClass());
boolean supportsNameAttribute = Util.instanceSupportsNameAttribute(idClass);
final IRepository repository = RepositoryFactory.getRepository();
SortedSet<? extends DefinitionsChildId> allDefinitionsChildIds = repository.getAllDefinitionsChildIds(idClass);
JsonFactory jsonFactory = new JsonFactory();
jsonFactory.setCodec(BackendUtils.mapper);
StringWriter sw = new StringWriter();
try {
JsonGenerator jg = jsonFactory.createGenerator(sw);
// Refactoring could move this class to common and fill it here
if (grouped == null) {
jg.writeStartArray();
for (DefinitionsChildId id : allDefinitionsChildIds) {
jg.writeStartObject();
jg.writeStringField("namespace", id.getNamespace().getDecoded());
jg.writeStringField("id", id.getXmlId().getDecoded());
if (supportsNameAttribute) {
AbstractComponentInstanceResource componentInstaceResource = AbstractComponentsResource.getComponentInstaceResource(id);
String name = ((IHasName) componentInstaceResource).getName();
jg.writeStringField("name", name);
} else {
// used for winery-qNameSelector to avoid an if there
jg.writeStringField("name", id.getXmlId().getDecoded());
}
jg.writeStringField("qName", id.getQName().toString());
if (full != null) {
try {
jg.writeFieldName("full");
jg.writeObject(BackendUtils.getDefinitionsHavingCorrectImports(repository, id));
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
jg.writeEndObject();
}
jg.writeEndArray();
} else {
jg.writeStartArray();
Map<Namespace, ? extends List<? extends DefinitionsChildId>> groupedIds = allDefinitionsChildIds.stream().collect(Collectors.groupingBy(id -> id.getNamespace()));
groupedIds.keySet().stream().sorted().forEach(namespace -> {
try {
jg.writeStartObject();
jg.writeStringField("id", namespace.getEncoded());
jg.writeStringField("text", namespace.getDecoded());
jg.writeFieldName("children");
jg.writeStartArray();
groupedIds.get(namespace).forEach(id -> {
try {
jg.writeStartObject();
String text;
if (supportsNameAttribute) {
AbstractComponentInstanceResource componentInstaceResource = AbstractComponentsResource.getComponentInstaceResource(id);
text = ((IHasName) componentInstaceResource).getName();
} else {
text = id.getXmlId().getDecoded();
}
jg.writeStringField("id", id.getQName().toString());
jg.writeStringField("text", text);
if (full != null) {
try {
jg.writeFieldName("full");
jg.writeObject(BackendUtils.getDefinitionsHavingCorrectImports(repository, id));
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
jg.writeEndObject();
} catch (IOException e) {
AbstractComponentsResource.LOGGER.error("Could not create JSON", e);
}
});
jg.writeEndArray();
jg.writeEndObject();
} catch (IOException e) {
AbstractComponentsResource.LOGGER.error("Could not create JSON", e);
}
});
jg.writeEndArray();
}
jg.close();
} catch (Exception e) {
AbstractComponentsResource.LOGGER.error(e.getMessage(), e);
return "[]";
}
return sw.toString();
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class AbstractComponentsWithTypeReferenceResource method onJsonPost.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(QNameWithTypeApiData jsonData) {
// only check for type parameter as namespace and name are checked in super.onPost
if (StringUtils.isEmpty(jsonData.type)) {
return Response.status(Status.BAD_REQUEST).build();
}
ResourceCreationResult creationResult = super.onPost(jsonData.namespace, jsonData.localname);
if (!creationResult.isSuccess()) {
return creationResult.getResponse();
}
if (creationResult.getStatus().equals(Status.CREATED)) {
final DefinitionsChildId id = (DefinitionsChildId) creationResult.getId();
final IRepository repository = RepositoryFactory.getRepository();
final Definitions definitions = repository.getDefinitions(id);
final TExtensibleElements element = definitions.getElement();
((HasType) element).setType(jsonData.type);
if (id instanceof EntityTemplateId) {
BackendUtils.initializeProperties(repository, (TEntityTemplate) element);
}
try {
BackendUtils.persist(id, definitions);
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
return creationResult.getResponse();
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class AvailableSuperclassesApiData method generateList.
private void generateList(Class<? extends DefinitionsChildId> clazz, DefinitionsChildId classToExclude) {
SortedSet<? extends DefinitionsChildId> allDefinitionsChildIds = RepositoryFactory.getRepository().getAllDefinitionsChildIds(clazz);
if (classToExclude != null) {
allDefinitionsChildIds.remove(classToExclude);
}
this.classes = new ArrayList<>();
for (DefinitionsChildId id : allDefinitionsChildIds) {
NameAndQNameApiData q = new NameAndQNameApiData(id);
this.classes.add(q);
}
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class EntityTypeResource method getListOfAllInstances.
/**
* Used by children to implement getListOfAllInstances()
*/
protected SortedSet<Select2OptGroup> getListOfAllInstances(Class<? extends DefinitionsChildId> clazz) throws RepositoryCorruptException {
Select2DataWithOptGroups data = new Select2DataWithOptGroups();
Collection<? extends DefinitionsChildId> instanceIds = RepositoryFactory.getRepository().getAllElementsReferencingGivenType(clazz, this.id.getQName());
for (DefinitionsChildId instanceId : instanceIds) {
String groupText = instanceId.getNamespace().getDecoded();
String text = BackendUtils.getName(instanceId);
data.add(groupText, instanceId.getQName().toString(), text);
}
return data.asSortedSet();
}
Aggregations