Search in sources :

Example 1 with NonUniqueResultException

use of eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException in project hale by halestudio.

the class TemplateUploadForm method onUploadSuccess.

/**
 * Called after a successful upload.
 *
 * @param form the form
 * @param templateId the template identifier
 * @param projectInfo the project info
 * @param updateInfo if for an updated template, the template information
 *            should be updated from the project
 */
protected void onUploadSuccess(Form<?> form, String templateId, ProjectInfo projectInfo, boolean updateInfo) {
    boolean newTemplate = TemplateUploadForm.this.templateId == null;
    OrientGraph graph = DatabaseHelper.getGraph();
    try {
        Template template = Template.getByTemplateId(graph, templateId);
        if (template == null) {
            form.error("Template could not be created");
            return;
        }
        if (newTemplate) {
            // created template was a new template
            // associate user as owner to template
            String login = UserUtil.getLogin();
            if (login != null) {
                User user = User.getByLogin(graph, login);
                graph.addEdge(null, template.getV(), user.getV(), "owner");
            }
            // forward to page to fill in template information
            setResponsePage(new NewTemplatePage(templateId));
        } else {
            // created template already existed
            // set last updated
            template.setLastUpdate(new Date());
            // update template info from project info
            if (updateInfo) {
                template.setName(projectInfo.getName());
                template.setAuthor(projectInfo.getAuthor());
                template.setDescription(projectInfo.getDescription());
            }
            // forward to template page
            setResponsePage(TemplatePage.class, new PageParameters().set(0, templateId));
        }
    } catch (NonUniqueResultException e) {
        form.error("Internal error");
        log.error("Duplicate template or user");
    } finally {
        graph.shutdown();
    }
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) User(eu.esdihumboldt.hale.server.model.User) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) NewTemplatePage(eu.esdihumboldt.hale.server.templates.war.pages.NewTemplatePage) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) Date(java.util.Date) Template(eu.esdihumboldt.hale.server.model.Template)

Example 2 with NonUniqueResultException

use of eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException in project hale by halestudio.

the class EditTemplatePage method addControls.

@Override
protected void addControls() {
    StringValue idParam = getPageParameters().get(0);
    if (!idParam.isNull() && !idParam.isEmpty()) {
        String templateId = idParam.toString();
        OrientGraph graph = DatabaseHelper.getGraph();
        try {
            Template template = null;
            try {
                template = Template.getByTemplateId(graph, templateId);
            } catch (NonUniqueResultException e) {
                log.error("Duplicate template representation: " + templateId, e);
            }
            if (template != null) {
                // get associated user
                Vertex v = template.getV();
                Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
                if (// user is admin
                UserUtil.isAdmin() || // or user is owner
                (owners.hasNext() && UserUtil.getLogin().equals(new User(owners.next(), graph).getLogin()))) {
                    add(new TemplateForm("edit-form", false, templateId));
                } else {
                    throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_FORBIDDEN);
                }
            } else {
                throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Template not found.");
            }
        } finally {
            graph.shutdown();
        }
    } else
        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_BAD_REQUEST, "Template identifier must be specified.");
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) Vertex(com.tinkerpop.blueprints.Vertex) User(eu.esdihumboldt.hale.server.model.User) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) TemplateForm(eu.esdihumboldt.hale.server.templates.war.components.TemplateForm) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) StringValue(org.apache.wicket.util.string.StringValue) Template(eu.esdihumboldt.hale.server.model.Template)

Example 3 with NonUniqueResultException

use of eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException in project hale by halestudio.

the class TemplateScavengerImpl method onAdd.

@Override
protected void onAdd(TemplateProject reference, String resourceId) {
    reference.update(null);
    Template template;
    try {
        // get existing representation in database
        template = Template.getByTemplateId(graph.get(), resourceId);
    } catch (NonUniqueResultException e) {
        log.error("Duplicate template representation in database");
        return;
    }
    if (template != null) {
        // update valid status
        boolean valid = reference.isValid();
        template.setValid(valid);
        log.info("Updating template {} - {}", resourceId, (valid) ? ("valid") : ("invalid"));
    } else {
        /*
			 * Only create a new database reference if the project actually is
			 * valid
			 */
        if (reference.isValid()) {
            ProjectInfo info = reference.getProjectInfo();
            // create new template representation in DB
            template = Template.create(graph.get());
            // populated with resource ID and values from project
            template.setTemplateId(resourceId);
            template.setName(info.getName());
            template.setAuthor(info.getAuthor());
            template.setDescription(info.getDescription());
            template.setValid(true);
            Date now = new Date();
            template.setCreated(now);
            template.setLastUpdate(now);
            log.info("Creating database representation for template {}", resourceId);
        }
    }
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) ProjectInfo(eu.esdihumboldt.hale.common.core.io.project.ProjectInfo) Date(java.util.Date) Template(eu.esdihumboldt.hale.server.model.Template)

Example 4 with NonUniqueResultException

use of eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException in project hale by halestudio.

the class TemplateScavengerImpl method onRemove.

@Override
protected void onRemove(TemplateProject reference, String resourceId) {
    // invalidate database reference (if it exists)
    OrientGraph g = graph.get();
    boolean cleanUp = g == null;
    if (g == null) {
        g = DatabaseHelper.getGraph();
    }
    try {
        Template template = Template.getByTemplateId(g, resourceId);
        if (template != null) {
            template.setValid(false);
            log.info("Template {} was removed - updating status to invalid", resourceId);
        }
    } catch (NonUniqueResultException e) {
        log.error("Duplicate template representation in database");
    } finally {
        if (cleanUp) {
            g.shutdown();
        }
    }
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) Template(eu.esdihumboldt.hale.server.model.Template)

Example 5 with NonUniqueResultException

use of eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException in project hale by halestudio.

the class UserDetailsServiceImpl method loadUserDetails.

@Override
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {
    OrientGraph graph = DatabaseHelper.getGraph();
    try {
        User user;
        try {
            user = User.getByLogin(graph, token.getIdentityUrl());
        } catch (NonUniqueResultException e) {
            // multiple users w/ same login?!
            log.error("Found multiple user with same identity URL: " + token.getIdentityUrl(), e);
            throw new IllegalStateException("Multiple users found for login");
        }
        boolean newUser = false;
        if (user == null) {
            // if using OpenID every user is automatically registered
            // create a default user
            user = User.create(graph);
            user.setLogin(token.getIdentityUrl());
            user.setPassword("");
            newUser = true;
            // try to retrieve info from attribute exchange
            for (OpenIDAttribute attribute : token.getAttributes()) {
                if (attribute.getCount() >= 0) {
                    switch(attribute.getName()) {
                        // fall through
                        case "email":
                        case "emailAlt":
                            user.setEmail(attribute.getValues().get(0));
                            break;
                        case "firstName":
                            user.setName(attribute.getValues().get(0));
                            break;
                        case "lastName":
                            user.setSurname(attribute.getValues().get(0));
                            break;
                    }
                }
            }
            log.info("Create default user for Open ID " + token.getIdentityUrl());
        }
        return createUserDetails(user, newUser);
    } finally {
        graph.shutdown();
    }
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) OpenIDAttribute(org.springframework.security.openid.OpenIDAttribute) ExtendedUser(eu.esdihumboldt.hale.server.security.util.impl.ExtendedUser) User(eu.esdihumboldt.hale.server.model.User) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph)

Aggregations

NonUniqueResultException (eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)7 Template (eu.esdihumboldt.hale.server.model.Template)7 User (eu.esdihumboldt.hale.server.model.User)6 Vertex (com.tinkerpop.blueprints.Vertex)3 AbortWithHttpErrorCodeException (org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException)3 StringValue (org.apache.wicket.util.string.StringValue)3 Date (java.util.Date)2 Label (org.apache.wicket.markup.html.basic.Label)2 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)2 PopoverConfig (de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig)1 ProjectInfo (eu.esdihumboldt.hale.common.core.io.project.ProjectInfo)1 ExtendedUser (eu.esdihumboldt.hale.server.security.util.impl.ExtendedUser)1 DeleteTemplateLink (eu.esdihumboldt.hale.server.templates.war.components.DeleteTemplateLink)1 ProjectURLPopover (eu.esdihumboldt.hale.server.templates.war.components.ProjectURLPopover)1 ResourcesPanel (eu.esdihumboldt.hale.server.templates.war.components.ResourcesPanel)1 TemplateForm (eu.esdihumboldt.hale.server.templates.war.components.TemplateForm)1 TemplateUploadForm (eu.esdihumboldt.hale.server.templates.war.components.TemplateUploadForm)1 NewTemplatePage (eu.esdihumboldt.hale.server.templates.war.pages.NewTemplatePage)1 HTMLPopoverBehavior (eu.esdihumboldt.hale.server.webapp.components.bootstrap.HTMLPopoverBehavior)1