use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class ImportUtils method getTheImport.
/**
* SIDE EFFECT: persists the import when something is changed
*/
public static Optional<TImport> getTheImport(GenericImportId id) {
Objects.requireNonNull(id);
TImport theImport;
boolean needsPersistence = false;
final IRepository repository = RepositoryFactory.getRepository();
final Definitions definitions = repository.getDefinitions(id);
if (!repository.exists(id)) {
return Optional.empty();
}
if (definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().isEmpty()) {
// definitions exist
// we have to manually assign our import right
theImport = definitions.getImport().get(0);
} else {
// someone created a new import
// store it locally
theImport = (TImport) definitions.getElement();
// undo the side effect of adding it at the wrong place at TDefinitions
definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().clear();
// add import at the right place
definitions.getImport().add(theImport);
// Super class has persisted the definitions
// We have to persist the new variant
needsPersistence = true;
}
if (theImport.getLocation() == null) {
// invalid import -- try to synchronize with storage
SortedSet<RepositoryFileReference> containedFiles = repository.getContainedFiles(id);
// we are only interested in the non-.definitions
for (RepositoryFileReference ref : containedFiles) {
if (!ref.getFileName().endsWith(".definitions")) {
// associated file found
// set the filename of the import to the found xsd
// TODO: no more validity checks are done currently. In the case of XSD: targetNamespace matches, not more than one xsd
theImport.setLocation(ref.getFileName());
needsPersistence = true;
break;
}
}
}
if (needsPersistence) {
try {
BackendUtils.persist(id, definitions);
} catch (IOException e) {
LOGGER.error("Could not persist changes", e);
}
}
return Optional.of(theImport);
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class SelfServiceMetaDataUtils method ensureDataXmlExists.
public static void ensureDataXmlExists(SelfServiceMetaDataId id) throws IOException {
RepositoryFileReference data_xml_ref = getDataXmlRef(id);
if (!RepositoryFactory.getRepository().exists(data_xml_ref)) {
final Application application = new Application();
BackendUtils.persist(application, data_xml_ref, MediaTypes.MEDIATYPE_TEXT_XML);
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class FilebasedRepository method getContainedFiles.
@Override
public SortedSet<RepositoryFileReference> getContainedFiles(GenericId id) {
Path dir = this.id2AbsolutePath(id);
SortedSet<RepositoryFileReference> res = new TreeSet<>();
if (!Files.exists(dir)) {
return res;
}
assert (Files.isDirectory(dir));
final OnlyNonHiddenFiles onlyNonHiddenFiles = new OnlyNonHiddenFiles();
try {
Files.walk(dir).filter(f -> {
try {
return onlyNonHiddenFiles.accept(f);
} catch (IOException e) {
LOGGER.debug("Error during crawling", e);
return false;
}
}).map(f -> {
final Path relativePath = dir.relativize(f.getParent());
if (relativePath.toString().isEmpty()) {
return new RepositoryFileReference(id, f.getFileName().toString());
} else {
return new RepositoryFileReference(id, relativePath, f.getFileName().toString());
}
}).forEach(ref -> res.add(ref));
} catch (IOException e1) {
LOGGER.debug("Error during crawling", e1);
}
return res;
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class FilebasedRepository method rename.
@Override
public void rename(DefinitionsChildId oldId, DefinitionsChildId newId) throws IOException {
Objects.requireNonNull(oldId);
Objects.requireNonNull(newId);
if (oldId.equals(newId)) {
// we do not do anything - even not throwing an error
return;
}
Definitions definitions = this.getDefinitions(oldId);
RepositoryFileReference oldRef = BackendUtils.getRefOfDefinitions(oldId);
RepositoryFileReference newRef = BackendUtils.getRefOfDefinitions(newId);
// oldRef points to the definitions file,
// getParent() returns the directory
// we need toFile(), because we rely on FileUtils.moveDirectoryToDirectory
File oldDir = this.id2AbsolutePath(oldRef.getParent()).toFile();
File newDir = this.id2AbsolutePath(newRef.getParent()).toFile();
org.apache.commons.io.FileUtils.moveDirectory(oldDir, newDir);
// Update definitions and store it
// This also updates the definitions of componentInstanceResource
BackendUtils.updateWrapperDefinitions(newId, definitions);
// This works, because the definitions object here is the same as the definitions object treated at copyIdToFields
// newId has to be passed, because the id is final at AbstractComponentInstanceResource
BackendUtils.copyIdToFields((HasIdInIdOrNameField) definitions.getElement(), newId);
try {
BackendUtils.persist(definitions, newRef, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
} catch (InvalidPathException e) {
LOGGER.debug("Invalid path during write", e);
// QUICK FIX
// Somewhere, the first letter is deleted --> /odetypes/http%3A%2F%2Fwww.example.org%2F05/
// We just ignore it for now
}
}
use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.
the class CsarExporter method addSelfServiceMetaData.
/**
* Adds all self service meta data to the targetDir
*
* @param targetDir the directory in the CSAR where to put the content to
* @param refMap is used later to create the CSAR
*/
private void addSelfServiceMetaData(IRepository repository, ServiceTemplateId entryId, String targetDir, Map<RepositoryFileReference, String> refMap) throws IOException {
final SelfServiceMetaDataId selfServiceMetaDataId = new SelfServiceMetaDataId(entryId);
// This method is also called if the directory SELFSERVICE-Metadata exists without content and even if the directory does not exist at all,
// but the ServiceTemplate itself exists.
// The current assumption is that this is enough for an existence.
// Thus, we have to take care of the case of an empty directory and add a default data.xml
SelfServiceMetaDataUtils.ensureDataXmlExists(selfServiceMetaDataId);
refMap.put(SelfServiceMetaDataUtils.getDataXmlRef(selfServiceMetaDataId), targetDir + "data.xml");
// The schema says that the images have to exist
// However, at a quick modeling, there might be no images
// Therefore, we check for existence
final RepositoryFileReference iconJpgRef = SelfServiceMetaDataUtils.getIconJpgRef(selfServiceMetaDataId);
if (repository.exists(iconJpgRef)) {
refMap.put(iconJpgRef, targetDir + "icon.jpg");
}
final RepositoryFileReference imageJpgRef = SelfServiceMetaDataUtils.getImageJpgRef(selfServiceMetaDataId);
if (repository.exists(imageJpgRef)) {
refMap.put(imageJpgRef, targetDir + "image.jpg");
}
Application application = SelfServiceMetaDataUtils.getApplication(selfServiceMetaDataId);
// clear CSAR name as this may change.
application.setCsarName(null);
// hack for the OpenTOSCA container to display something
application.setVersion("1.0");
List<String> authors = application.getAuthors();
if (authors.isEmpty()) {
authors.add("Winery");
}
// make the patches to data.xml permanent
try {
BackendUtils.persist(application, SelfServiceMetaDataUtils.getDataXmlRef(selfServiceMetaDataId), MediaTypes.MEDIATYPE_TEXT_XML);
} catch (IOException e) {
LOGGER.error("Could not persist patches to data.xml", e);
}
Options options = application.getOptions();
if (options != null) {
SelfServiceMetaDataId id = new SelfServiceMetaDataId(entryId);
for (ApplicationOption option : options.getOption()) {
String url = option.getIconUrl();
if (Util.isRelativeURI(url)) {
putRefIntoRefMap(targetDir, refMap, repository, id, url);
}
url = option.getPlanInputMessageUrl();
if (Util.isRelativeURI(url)) {
putRefIntoRefMap(targetDir, refMap, repository, id, url);
}
}
}
}
Aggregations