Search in sources :

Example 1 with HaleConnectProjectInfo

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo in project hale by halestudio.

the class ChooseHaleConnectProjectWizardPage method createContent.

/**
 * @see eu.esdihumboldt.hale.ui.util.wizard.ConfigurationWizardPage#createContent(org.eclipse.swt.widgets.Composite)
 */
@Override
protected void createContent(Composite parent) {
    Composite page = new Composite(parent, SWT.NONE);
    page.setLayout(new GridLayout(4, false));
    keywordFilter = new Text(page, SWT.SEARCH | SWT.ICON_SEARCH | SWT.BORDER | SWT.ICON_CANCEL);
    keywordFilter.setMessage("Enter search text here...");
    GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(keywordFilter);
    ownerFilter = new ComboViewer(page);
    GridDataFactory.fillDefaults().grab(false, false).applyTo(ownerFilter.getControl());
    ownerFilter.setContentProvider(new IStructuredContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            new Object();
        }

        @Override
        public void dispose() {
        // 
        }

        @Override
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof List<?>) {
                List<?> elements = (List<?>) inputElement;
                return elements.toArray();
            } else if (inputElement instanceof Object[]) {
                return (Object[]) inputElement;
            }
            return new Object[] {};
        }
    });
    ownerFilter.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            if (element instanceof OwnerFilterEntry) {
                return ((OwnerFilterEntry) element).getLabel();
            }
            return super.getText(element);
        }
    });
    List<OwnerFilterEntry> availableFilters = new ArrayList<>();
    availableFilters.add(NULL_FILTER);
    availableFilters.add(new OwnerFilterEntry(new Owner[] { new Owner(OwnerType.USER, haleConnect.getSession().getUserId()) }, "My projects"));
    List<Owner> orgs = new ArrayList<>();
    for (String orgId : haleConnect.getSession().getOrganisationIds()) {
        orgs.add(new Owner(OwnerType.ORGANISATION, orgId));
    }
    if (!orgs.isEmpty()) {
        availableFilters.add(new OwnerFilterEntry(orgs.toArray(new Owner[] {}), "My organisations' projects"));
    }
    ownerFilter.setInput(availableFilters);
    ownerFilter.setSelection(new StructuredSelection(NULL_FILTER));
    Composite tableComposite = new Composite(page, SWT.NONE);
    TableColumnLayout columnLayout = new TableColumnLayout();
    tableComposite.setLayout(columnLayout);
    projects = new TableViewer(tableComposite, SWT.V_SCROLL | SWT.BORDER | SWT.H_SCROLL | SWT.SINGLE | SWT.FULL_SELECTION);
    projects.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            update();
        }
    });
    TableViewerColumn nameColumn = new TableViewerColumn(projects, SWT.LEAD);
    nameColumn.getColumn().setText("Project name");
    nameColumn.setLabelProvider(new ColumnLabelProvider() {

        /**
         * @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
         */
        @Override
        public String getText(Object element) {
            if (element instanceof HaleConnectProjectInfo) {
                return ((HaleConnectProjectInfo) element).getName();
            }
            return element.toString();
        }
    });
    columnLayout.setColumnData(nameColumn.getColumn(), new ColumnWeightData(45, 200, true));
    TableViewerColumn authorColumn = new TableViewerColumn(projects, SWT.LEAD);
    authorColumn.getColumn().setText("Author");
    authorColumn.setLabelProvider(new ColumnLabelProvider() {

        /**
         * @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
         */
        @Override
        public String getText(Object element) {
            if (element instanceof HaleConnectProjectInfo) {
                return ((HaleConnectProjectInfo) element).getAuthor();
            }
            return element.toString();
        }
    });
    columnLayout.setColumnData(authorColumn.getColumn(), new ColumnWeightData(20, 50, true));
    TableViewerColumn ownerColumn = new TableViewerColumn(projects, SWT.LEAD);
    ownerColumn.getColumn().setText("Owner");
    ownerColumn.setLabelProvider(new ColumnLabelProvider() {

        /**
         * @see org.eclipse.jface.viewers.ColumnLabelProvider#getText(java.lang.Object)
         */
        @Override
        public String getText(Object element) {
            if (element instanceof HaleConnectProjectInfo) {
                Owner owner = ((HaleConnectProjectInfo) element).getOwner();
                if (owner.isUser()) {
                    try {
                        HaleConnectUserInfo user = haleConnect.getUserInfo(owner.getId());
                        if (!StringUtils.isEmpty(user.getFullName())) {
                            return user.getFullName();
                        } else if (!StringUtils.isEmpty(user.getScreenName())) {
                            return user.getScreenName();
                        } else {
                            return MessageFormat.format("<user {0}>", user.getUserId());
                        }
                    } catch (HaleConnectException e) {
                        log.error(e.getMessage(), e);
                        return MessageFormat.format("<user {0}>", owner.getId());
                    }
                } else if (owner.isOrganisation()) {
                    try {
                        return haleConnect.getOrganisationInfo(owner.getId()).getName();
                    } catch (HaleConnectException e) {
                        log.error(e.getMessage(), e);
                        return MessageFormat.format("<organisation {0}>", owner.getId());
                    }
                } else {
                    return "<unknown owner type> ID: " + owner.getId();
                }
            }
            return element.toString();
        }
    });
    columnLayout.setColumnData(ownerColumn.getColumn(), new ColumnWeightData(35, 100, true));
    final Table projectsTable = projects.getTable();
    projectsTable.setHeaderVisible(true);
    projectsTable.setLinesVisible(true);
    tableComposite.setLayoutData(GridDataFactory.fillDefaults().span(4, 1).grab(true, true).create());
    projects.setContentProvider(ArrayContentProvider.getInstance());
    populateProjects();
    final ProjectFilter projectFilter = new ProjectFilter(projects);
    projects.addFilter(projectFilter);
    ownerFilter.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            if (event.getSelection() instanceof StructuredSelection) {
                StructuredSelection selection = (StructuredSelection) event.getSelection();
                if (selection.getFirstElement() instanceof OwnerFilterEntry) {
                    OwnerFilterEntry selectedFilter = (OwnerFilterEntry) selection.getFirstElement();
                    if (selectedFilter.equals(NULL_FILTER)) {
                        projectFilter.clearOwnerFilter();
                    } else {
                        projectFilter.filterByOwners(Arrays.asList(selectedFilter.getOwner()));
                    }
                }
            }
        }
    });
    keywordFilter.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            projectFilter.setSearchText(keywordFilter.getText());
        }
    });
    final Button refresh = new Button(page, SWT.FLAT);
    refresh.setText("Refresh");
    GridDataFactory.fillDefaults().span(1, 1).grab(false, false).applyTo(refresh);
    refresh.addSelectionListener(new SelectionAdapter() {

        /**
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        @Override
        public void widgetSelected(SelectionEvent e) {
            populateProjects();
        }
    });
    ownerFilter.getControl().setFocus();
    setControl(page);
    update();
}
Also used : ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) Owner(eu.esdihumboldt.hale.io.haleconnect.Owner) ModifyListener(org.eclipse.swt.events.ModifyListener) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) TableViewer(org.eclipse.jface.viewers.TableViewer) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) GridLayout(org.eclipse.swt.layout.GridLayout) ModifyEvent(org.eclipse.swt.events.ModifyEvent) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) ComboViewer(org.eclipse.jface.viewers.ComboViewer) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) LabelProvider(org.eclipse.jface.viewers.LabelProvider) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 2 with HaleConnectProjectInfo

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo in project hale by halestudio.

the class HaleConnectTarget method prefillTargetProject.

/**
 */
private void prefillTargetProject() {
    if (!haleConnect.isLoggedIn()) {
        return;
    }
    ProjectInfoService pis = HaleUI.getServiceProvider().getService(ProjectInfoService.class);
    String projectUrnProperty = pis.getProperty(HaleConnectProjectReader.HALECONNECT_URN_PROPERTY).getStringRepresentation();
    if (projectUrnProperty != null) {
        // If project was loaded from hale connect, prefill project name
        URI projectUrn = URI.create(projectUrnProperty);
        if (HaleConnectUrnBuilder.isValidProjectUrn(projectUrn)) {
            String projectId = HaleConnectUrnBuilder.extractProjectId(projectUrn);
            Owner owner = HaleConnectUrnBuilder.extractProjectOwner(projectUrn);
            try {
                if (!haleConnect.testProjectPermission(HaleConnectService.PERMISSION_EDIT, owner, projectId)) {
                    return;
                }
                HaleConnectProjectInfo projectInfo = haleConnect.getProject(owner, projectId);
                if (projectInfo != null) {
                    targetProject = new HaleConnectProjectConfig();
                    targetProject.setOwner(owner);
                    targetProject.setProjectId(projectId);
                    targetProject.setProjectName(projectInfo.getName());
                    targetProject.setLastModified(projectInfo.getLastModified());
                    newProject.setSelection(false);
                    updateProject.setSelection(true);
                    projectName.setStringValue(projectInfo.getName());
                }
            } catch (Throwable t) {
                // Non-fatal
                log.warn(MessageFormat.format("Unable to prefill target project: {0}", t.getMessage()), t);
            }
        }
    }
}
Also used : Owner(eu.esdihumboldt.hale.io.haleconnect.Owner) ProjectInfoService(eu.esdihumboldt.hale.common.core.io.project.ProjectInfoService) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) URI(java.net.URI)

Example 3 with HaleConnectProjectInfo

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo in project hale by halestudio.

the class ProjectFilter method select.

/**
 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer,
 *      java.lang.Object, java.lang.Object)
 */
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
    if (!(element instanceof HaleConnectProjectInfo)) {
        // fast exit
        return true;
    }
    HaleConnectProjectInfo pi = (HaleConnectProjectInfo) element;
    boolean match = visibleOwners.isEmpty() || visibleOwners.contains(pi.getOwner());
    match &= StringUtils.isEmpty(searchText) || containsSearchText(pi, searchText);
    return match;
}
Also used : HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo)

Example 4 with HaleConnectProjectInfo

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo in project hale by halestudio.

the class HaleConnectServiceImpl method loadProject.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#loadProject(Owner,
 *      String)
 */
@Override
public LocatableInputSupplier<InputStream> loadProject(Owner owner, String projectId) throws HaleConnectException {
    if (!isLoggedIn()) {
        throw new IllegalStateException("Not logged in.");
    }
    URI location = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
    HaleConnectProjectInfo projectInfo = getProject(owner, projectId);
    if (projectInfo == null) {
        throw new HaleConnectException(MessageFormat.format("Project does not exist: {0}", location.toString()));
    }
    return new HaleConnectInputSupplier(location, this.getSession().getToken(), this);
}
Also used : HaleConnectInputSupplier(eu.esdihumboldt.hale.io.haleconnect.HaleConnectInputSupplier) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) URI(java.net.URI)

Example 5 with HaleConnectProjectInfo

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo 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;
}
Also used : Owner(eu.esdihumboldt.hale.io.haleconnect.Owner) LocatableURI(eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) MutableTargetIOReport(eu.esdihumboldt.hale.common.core.io.report.MutableTargetIOReport) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) URI(java.net.URI) LocatableURI(eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) OwnerType(eu.esdihumboldt.hale.io.haleconnect.OwnerType) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileOutputStream(java.io.FileOutputStream) NoStreamOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.NoStreamOutputSupplier) File(java.io.File) MutableTargetIOReport(eu.esdihumboldt.hale.common.core.io.report.MutableTargetIOReport)

Aggregations

HaleConnectProjectInfo (eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo)7 HaleConnectException (eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)4 Owner (eu.esdihumboldt.hale.io.haleconnect.Owner)3 URI (java.net.URI)3 HaleConnectUserInfo (eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 ProjectInfoService (eu.esdihumboldt.hale.common.core.io.project.ProjectInfoService)1 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)1 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)1 MutableTargetIOReport (eu.esdihumboldt.hale.common.core.io.report.MutableTargetIOReport)1 LocatableURI (eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI)1 NoStreamOutputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.NoStreamOutputSupplier)1 HaleConnectInputSupplier (eu.esdihumboldt.hale.io.haleconnect.HaleConnectInputSupplier)1 HaleConnectOrganisationInfo (eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo)1 OwnerType (eu.esdihumboldt.hale.io.haleconnect.OwnerType)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1