Search in sources :

Example 6 with Template

use of eu.esdihumboldt.hale.server.model.Template in project hale by halestudio.

the class DeleteTemplateLink method onClick.

@Override
public void onClick() {
    // delete from file system
    scavenger.deleteResource(templateId);
    // afterwards trigger the database deletion
    OrientGraph graph = DatabaseHelper.getGraph();
    try {
        Template template = Template.getByTemplateId(graph, templateId);
        if (template == null) {
            error("Template not found");
            return;
        }
        // delete from database
        template.delete();
    } catch (NonUniqueResultException e) {
        error("Internal error");
        log.error("Duplicate template");
    } finally {
        graph.shutdown();
    }
    setResponsePage(TemplatesPage.class);
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) Template(eu.esdihumboldt.hale.server.model.Template)

Example 7 with Template

use of eu.esdihumboldt.hale.server.model.Template in project hale by halestudio.

the class TemplatePage method addControls.

@SuppressWarnings("serial")
@Override
protected void addControls(boolean loggedIn) {
    super.addControls(loggedIn);
    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) {
                // name
                Label name = new Label("name", template.getName());
                add(name);
                // download
                String href = TemplateLocations.getTemplateDownloadUrl(templateId);
                ExternalLink download = new ExternalLink("download", href);
                add(download);
                // project location
                WebMarkupContainer project = new WebMarkupContainer("project");
                project.add(AttributeModifier.replace("value", TemplateLocations.getTemplateProjectUrl(scavenger, templateId)));
                add(project);
                // author
                Label author = new Label("author", template.getAuthor());
                add(author);
                // edit-buttons container
                WebMarkupContainer editButtons = new WebMarkupContainer("edit-buttons");
                editButtons.setVisible(false);
                add(editButtons);
                // deleteDialog container
                WebMarkupContainer deleteDialog = new WebMarkupContainer("deleteDialog");
                deleteDialog.setVisible(false);
                add(deleteDialog);
                // user
                String userName;
                Vertex v = template.getV();
                boolean showEditPart = UserUtil.isAdmin();
                Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
                if (owners.hasNext()) {
                    User user = new User(owners.next(), graph);
                    userName = UserUtil.getDisplayName(user);
                    showEditPart = loggedIn && UserUtil.getLogin().equals(user.getLogin());
                } else {
                    userName = "Unregistered user";
                }
                // edit buttons
                if (showEditPart) {
                    editButtons.setVisible(true);
                    deleteDialog.setVisible(true);
                    // edit
                    editButtons.add(new BookmarkablePageLink<>("edit", EditTemplatePage.class, new PageParameters().set(0, templateId)));
                    // update
                    editButtons.add(new BookmarkablePageLink<>("update", UpdateTemplatePage.class, new PageParameters().set(0, templateId)));
                    // delete
                    deleteDialog.add(new DeleteTemplateLink("delete", templateId));
                }
                Label user = new Label("user", userName);
                add(user);
                // description
                String descr = template.getDescription();
                String htmlDescr = null;
                if (descr == null || descr.isEmpty()) {
                    descr = "No description for the project template available.";
                } else {
                    // convert markdown to
                    htmlDescr = new PegDownProcessor(Extensions.AUTOLINKS | Extensions.SUPPRESS_ALL_HTML | Extensions.HARDWRAPS | Extensions.SMARTYPANTS).markdownToHtml(descr);
                }
                // description in pre
                Label description = new Label("description", descr);
                description.setVisible(htmlDescr == null);
                add(description);
                // description in div
                Label htmlDescription = new Label("html-description", htmlDescr);
                htmlDescription.setVisible(htmlDescr != null);
                htmlDescription.setEscapeModelStrings(false);
                add(htmlDescription);
                // invalid
                WebMarkupContainer statusInvalid = new WebMarkupContainer("invalid");
                statusInvalid.setVisible(!template.isValid());
                add(statusInvalid);
                // resources
                ResourcesPanel resources = new ResourcesPanel("resources", templateId);
                add(resources);
                // project popover
                WebMarkupContainer projectPopover = new WebMarkupContainer("project-popover");
                projectPopover.add(new HTMLPopoverBehavior(Model.of("Load template in HALE"), new PopoverConfig().withPlacement(Placement.bottom)) {

                    @Override
                    public Component newBodyComponent(String markupId) {
                        return new ProjectURLPopover(markupId);
                    }
                });
                add(projectPopover);
            } 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 : Vertex(com.tinkerpop.blueprints.Vertex) User(eu.esdihumboldt.hale.server.model.User) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ResourcesPanel(eu.esdihumboldt.hale.server.templates.war.components.ResourcesPanel) Label(org.apache.wicket.markup.html.basic.Label) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) PopoverConfig(de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) Template(eu.esdihumboldt.hale.server.model.Template) DeleteTemplateLink(eu.esdihumboldt.hale.server.templates.war.components.DeleteTemplateLink) StringValue(org.apache.wicket.util.string.StringValue) Component(org.apache.wicket.Component) NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) ProjectURLPopover(eu.esdihumboldt.hale.server.templates.war.components.ProjectURLPopover) HTMLPopoverBehavior(eu.esdihumboldt.hale.server.webapp.components.bootstrap.HTMLPopoverBehavior) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ExternalLink(org.apache.wicket.markup.html.link.ExternalLink) PegDownProcessor(org.pegdown.PegDownProcessor)

Example 8 with Template

use of eu.esdihumboldt.hale.server.model.Template in project hale by halestudio.

the class UpdateTemplatePage 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()))) {
                    // template name
                    add(new Label("name", template.getName()));
                    // upload form
                    add(new TemplateUploadForm("upload-form", 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) TemplateUploadForm(eu.esdihumboldt.hale.server.templates.war.components.TemplateUploadForm) Label(org.apache.wicket.markup.html.basic.Label) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) StringValue(org.apache.wicket.util.string.StringValue) Template(eu.esdihumboldt.hale.server.model.Template)

Example 9 with Template

use of eu.esdihumboldt.hale.server.model.Template in project hale by halestudio.

the class TemplatesAPI method getFile.

/**
 * Retrieve all templates.
 *
 * @param request the request
 * @param response the response
 */
@RequestMapping(value = "/all", method = RequestMethod.GET, produces = "application/json")
public void getFile(HttpServletRequest request, HttpServletResponse response) {
    OrientGraph graph = DatabaseHelper.getGraph();
    try {
        Iterable<Template> allTemplates = Template.findAll(graph);
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        JsonFactory jsonFactory = new JsonFactory();
        try (Writer writer = response.getWriter();
            JsonGenerator gen = jsonFactory.createGenerator(writer)) {
            gen.writeStartObject();
            gen.writeArrayFieldStart("templates");
            for (Template template : allTemplates) {
                if (template.isValid()) {
                    // only valid templates should be returned
                    gen.writeStartObject();
                    String id = template.getTemplateId();
                    gen.writeStringField("id", id);
                    gen.writeStringField("name", template.getName());
                    gen.writeStringField("project", TemplateLocations.getTemplateProjectUrl(scavenger, id));
                    gen.writeStringField("site", TemplateLocations.getTemplatePageUrl(id));
                    gen.writeEndObject();
                }
            }
            gen.writeEndArray();
            gen.writeEndObject();
        } catch (IOException e) {
            log.error("Error writing JSON response", e);
        }
    } finally {
        graph.shutdown();
    }
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) Writer(java.io.Writer) Template(eu.esdihumboldt.hale.server.model.Template) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Template (eu.esdihumboldt.hale.server.model.Template)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)7 NonUniqueResultException (eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException)7 User (eu.esdihumboldt.hale.server.model.User)4 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 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 PopoverConfig (de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig)1 ProjectInfo (eu.esdihumboldt.hale.common.core.io.project.ProjectInfo)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