use of eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI in project hale by halestudio.
the class HaleConnectProjectWriter method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
if (!haleConnect.isLoggedIn()) {
reporter.error("Must be logged in to hale connect to upload project.");
reporter.setSuccess(false);
return reporter;
}
URI location = null;
if (getTarget().getLocation() != null) {
location = getTarget().getLocation();
}
progress.begin("Saving project to hale connect", ProgressIndicator.UNKNOWN);
Project project = getProject();
URI projectUrn;
if (location == null) {
// was not shared before or creation of new project requested by
// user
boolean enableVersioning = getParameter(ENABLE_VERSIONING).as(Boolean.class);
boolean publicAccess = getParameter(SHARING_PUBLIC).as(Boolean.class);
String ownerTypeParameter = getParameter(OWNER_TYPE).as(String.class);
OwnerType ownerType;
try {
ownerType = OwnerType.fromJsonValue(ownerTypeParameter);
} catch (IllegalArgumentException e) {
throw new IOProviderConfigurationException(MessageFormat.format("Invalid owner type: {0}", ownerTypeParameter), e);
}
String ownerId;
switch(ownerType) {
case USER:
ownerId = haleConnect.getSession().getUserId();
break;
case ORGANISATION:
if (haleConnect.getSession().getOrganisationIds().isEmpty()) {
throw new IOProviderConfigurationException(MessageFormat.format("Owner type is set to ORGANISATION but user \"{0}\" is not associated with any organisation", haleConnect.getSession().getUsername()));
}
ownerId = haleConnect.getSession().getOrganisationIds().iterator().next();
break;
default:
throw new IOProviderConfigurationException(MessageFormat.format("Unknown owner type: {0}", ownerType));
}
Owner owner = new Owner(ownerType, ownerId);
String projectId;
try {
projectId = haleConnect.createProject(project.getName(), project.getAuthor(), owner, enableVersioning);
haleConnect.setProjectSharingOptions(projectId, owner, new SharingOptions(publicAccess));
} catch (HaleConnectException e) {
reporter.error("Error creating hale connect project", e);
reporter.setSuccess(false);
return reporter;
}
projectUrn = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
if (reporter instanceof MutableTargetIOReport) {
((MutableTargetIOReport) reporter).setTarget(new LocatableURI(prettifyTarget(projectUrn)));
}
} else if (!HaleConnectUrnBuilder.isValidProjectUrn(location)) {
throw new IOProviderConfigurationException(MessageFormat.format("Cannot write to location: {0}", location.toString()));
} else {
projectUrn = location;
writerMode = ProjectWriterMode.SAVE;
}
this.setTarget(new NoStreamOutputSupplier(projectUrn));
// save the hale connect project URN in the project properties
getProject().getProperties().put(HaleConnectProjectReader.HALECONNECT_URN_PROPERTY, Value.of(projectUrn.toString()));
// redirect project archive to temporary local file
File projectArchive = Files.createTempFile("hc-arc", ".zip").toFile();
IOReport report;
try (final FileOutputStream archiveStream = new FileOutputStream(projectArchive)) {
report = createProjectArchive(archiveStream, reporter, progress);
}
if (!report.isSuccess()) {
// exit when creating project archive failed
return report;
}
String projectId = HaleConnectUrnBuilder.extractProjectId(projectUrn);
Owner owner = HaleConnectUrnBuilder.extractProjectOwner(projectUrn);
boolean result;
try {
result = haleConnect.uploadProjectFile(projectId, owner, projectArchive, progress);
} catch (HaleConnectException e) {
switch(e.getStatusCode()) {
case 403:
/* Forbidden */
reporter.error(MessageFormat.format("You are not authorized to access project {0}", projectId), e);
break;
default:
reporter.error(MessageFormat.format("Error uploading hale connect project: {0}", e.getMessage()), e);
}
reporter.setSuccess(false);
return reporter;
}
// name
try {
haleConnect.setProjectName(projectId, owner, project.getName());
} catch (HaleConnectException e) {
// This is non-fatal
log.warn(MessageFormat.format("Unable to update project bucket name for project {0}: {1}", HaleConnectUrnBuilder.buildProjectUrn(owner, projectId).toString(), e.getMessage()), e);
}
try {
HaleConnectProjectInfo hcProjectInfo = haleConnect.getProject(owner, projectId);
if (hcProjectInfo != null) {
getProject().getProperties().put(HaleConnectProjectReader.HALECONNECT_LAST_MODIFIED_PROPERTY, Value.of(hcProjectInfo.getLastModified()));
}
} catch (HaleConnectException e) {
// This is non-fatal
log.warn(MessageFormat.format("Unable to get lastUpdated property for project {0}: {1}", HaleConnectUrnBuilder.buildProjectUrn(owner, projectId).toString(), e.getMessage()), e);
}
this.clientAccessUrl = HaleConnectUrnBuilder.buildClientAccessUrl(haleConnect.getBasePathManager().getBasePath(HaleConnectServices.WEB_CLIENT), owner, projectId);
this.projectUri = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
reporter.setSuccess(result);
return reporter;
}
use of eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI in project hale by halestudio.
the class HaleConnectProjectWriter method createReporter.
/**
* @see IOProvider#createReporter()
*/
@Override
public IOReporter createReporter() {
if (!(haleConnect instanceof BasePathResolver) || !HaleConnectUrnBuilder.isValidProjectUrn(getTarget().getLocation())) {
return super.createReporter();
}
try {
URI targetUri = getTarget().getLocation();
Locatable prettifiedTarget = new LocatableURI(prettifyTarget(targetUri));
return new DefaultIOReporter(prettifiedTarget, MessageFormat.format("{0} export", getTypeName()), getActionId(), true);
} catch (Throwable t) {
return super.createReporter();
}
}
use of eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI in project hale by halestudio.
the class WFSFeatureCollectionWriter method createFeatureCollectionElement.
private XmlElement createFeatureCollectionElement(WFSVersion version, XmlIndex targetIndex) {
String wfsNs = version.wfsNamespace;
XmlElement memberElement = null;
switch(version) {
case V1_1_0:
memberElement = targetIndex.getElements().get(new QName(GMLConstants.NS_GML, "_Feature"));
break;
case V2_0_0:
case V2_0_2:
for (XmlElement element : targetIndex.getElements().values()) {
if (element.getName().getLocalPart().equals("AbstractFeature") && element.getName().getNamespaceURI().startsWith(GMLConstants.GML_NAMESPACE_CORE)) {
memberElement = element;
break;
}
}
break;
}
if (memberElement == null) {
throw new IllegalStateException("Unable to identify member types for feature collection");
}
addValidationSchema(version.wfsNamespace, new LocatableURI(version.schemaLocation), "wfs");
List<XmlElement> memberElements = SubstitutionGroupProperty.collectSubstitutions(memberElement.getName(), memberElement.getType());
TypeDefinition fcType = WFSSchemaHelper.createFeatureCollectionType(version, memberElements);
return new XmlElement(new QName(wfsNs, "FeatureCollection"), fcType, null);
}
use of eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI in project hale by halestudio.
the class HaleConnectProjectReader method createReporter.
/**
* @see eu.esdihumboldt.hale.common.core.io.impl.AbstractImportProvider#createReporter()
*/
@Override
public IOReporter createReporter() {
if (!(getSource() instanceof HaleConnectInputSupplier) || !HaleConnectUrnBuilder.isValidProjectUrn(getSource().getLocation())) {
return super.createReporter();
}
try {
BasePathResolver resolver = ((HaleConnectInputSupplier) getSource()).getBasePathResolver();
URI sourceUri = getSource().getLocation();
Owner owner = HaleConnectUrnBuilder.extractProjectOwner(sourceUri);
String projectId = HaleConnectUrnBuilder.extractProjectId(sourceUri);
String clientBasePath = resolver.getBasePath(HaleConnectServices.WEB_CLIENT);
Locatable prettifiedTarget = new LocatableURI(HaleConnectUrnBuilder.buildClientAccessUrl(clientBasePath, owner, projectId));
return new DefaultIOReporter(prettifiedTarget, MessageFormat.format("{0} import", getTypeName()), getActionId(), true);
} catch (Throwable t) {
return super.createReporter();
}
}
Aggregations