Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for property file upload #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<body>
<wicket:panel>

<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span></button>
<h4 class="modal-title" wicket:id="title">[add or edit node]</h4>
</div><!-- ./modal-header -->

<div class="modal-body">

<form wicket:id="form" role="form">

<input wicket:id="file" type="file" class="form-control" placeholder="node key"/>
<div class="modal-footer">
<input type="button" wicket:id="submit" value="Upload"/>
</div>
</form>

</div><!-- ./modal-body -->

</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

</wicket:panel>
</body>
</html>
131 changes: 131 additions & 0 deletions src/main/java/org/github/etcd/viewer/html/node/AddFileModalPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.github.etcd.viewer.html.node;

import com.fasterxml.jackson.databind.annotation.JsonAppend.Prop;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.inject.Inject;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.github.etcd.service.EtcdProxyFactory;
import org.github.etcd.service.api.EtcdException;
import org.github.etcd.service.api.EtcdNode;
import org.github.etcd.service.api.EtcdProxy;
import org.github.etcd.viewer.html.modal.GenericModalPanel;

public class AddFileModalPanel extends GenericModalPanel {

private static final long serialVersionUID = -2070660918144252605L;
private FileUploadField uploadField;
private IModel<Boolean> updating;
private IModel<String> registry;

@Inject
private EtcdProxyFactory proxyFactory;


private Form<EtcdNode> form;
private Label title;

// public addFileModalPanel(String id) {
// this(id, new CompoundPropertyModel(makeModel()));
// }

public AddFileModalPanel(String id, IModel<EtcdNode> model,
IModel<String> registry, IModel<Boolean> updating) {
super(id, null);

this.updating = updating;
this.registry = registry;

add(title = new Label("title",
new StringResourceModel("editModal.title.updating.${}", updating, "Upload from Properties file")));
title.setOutputMarkupId(true);

form = new Form<EtcdNode>("form");

form.add(new AjaxFormSubmitBehavior("onSubmit") {
@Override
protected void onSubmit(AjaxRequestTarget target) {
}
});

AjaxButton ab = new AjaxButton("submit") {
protected void onSubmit(AjaxRequestTarget target, Form form) {

super.onSubmit(target, form);
// final ValueMap map = getModelObject();
// to show when model is updated
// map.put("textSeen", map.get("text"));
// map.put("submitSeen", "true");
final FileUpload upload = uploadField.getFileUpload();
// map.put("haveUpload", upload != null);
if (upload != null) {

String fileName = upload.getClientFileName();
if (fileName.endsWith(".properties")) {

Properties properties = new Properties();
try (EtcdProxy p = proxyFactory.getEtcdProxy(registry.getObject())) {
properties.load(upload.getInputStream());

List<EtcdNode> entries = new ArrayList<>();

properties.forEach((key, value) -> {

p.saveNode(
new EtcdNode((String) model.getObject().getKey() + "/" + key,
(String) value));
});

success("Added keys: " + properties);

} catch (EtcdException e) {
System.err.println("Caught error: " + e);
error(e.toString());
error(" - API error: " + e.getApiError());
error(" - " + e.getCause());
} catch (Exception e) {
System.err.println("Caught error: " + e);
error(e.toString());
error("error: " + e.getMessage());
error(" - " + e.getCause());
}
}
}

onFileSaved(target);

modalHide(target);
}
};

form.add(ab);
add(form);

uploadField = new FileUploadField("file");
form.add(uploadField);
form.setMultiPart(true);
}

@Override
public void beforeModalShow(AjaxRequestTarget target) {
target.add(title, form);

form.clearInput();
}

protected void onFileSaved(AjaxRequestTarget properties) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ <h4><span wicket:id="icon"></span> <span wicket:id="key"></span></h4>
</ol>

<div class="btn-group node-actions" role="group" aria-label="...">
<a wicket:id="addFile" href="#" class="btn btn-default tooltip-trigger" title="Upload from property file"><span class="fa fa-file"></span></a>
<a wicket:id="addNode" href="#" class="btn btn-default tooltip-trigger" title="Add another key-value or directory"><span class="fa fa-plus"></span></a>
<a wicket:id="editNode" href="#" class="btn btn-default tooltip-trigger" title="Edit current node"><span class="fa fa-pencil"></span></a>
<a wicket:id="deleteNode" href="#" class="btn btn-default tooltip-trigger" title="Delete current node"><span class="fa fa-trash"></span></a>
Expand Down Expand Up @@ -207,6 +208,7 @@ <h4><span wicket:id="icon"></span> <span wicket:id="key"></span></h4>

<div wicket:id="editNodeModal"></div>
<div wicket:id="deleteNodeModal"></div>
<div wicket:id="addFile"></div>


</wicket:panel>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/org/github/etcd/viewer/html/node/EtcdNodePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public int compare(EtcdNode o1, EtcdNode o2) {

private EditNodeModalPanel editNodeModal;
private DeleteNodeModalPanel deleteNodeModal;
private AddFileModalPanel addFileModalPanel;


private WebMarkupContainer breadcrumbAndActions;
private WebMarkupContainer contents;
Expand Down Expand Up @@ -195,6 +197,9 @@ protected void onNodeKeyUpdated(AjaxRequestTarget target) {
protected void onNodedSaved(AjaxRequestTarget target) {
}

protected void onFileSaved(AjaxRequestTarget target) {
}

protected void onNodedDeleted(AjaxRequestTarget target) {
}

Expand Down Expand Up @@ -233,6 +238,29 @@ protected void onNodeDeleted(AjaxRequestTarget target) {
EtcdNodePanel.this.onNodedDeleted(target);
}
});

add(addFileModalPanel = new AddFileModalPanel("addFile",
actionModel,
registry, updating) {
private static final long serialVersionUID = 1L;

@Override
protected void onFileSaved(AjaxRequestTarget target) {

super.onFileSaved(target);
//
// PageParameters params = ConvertUtils.getPageParameters(key.getObject());
// params.add("cluster", registry.getObject());
//
// setResponsePage(NavigationPage.class, params);
//
// key.setObject(key.getObject());

target.add(contents, breadcrumbAndActions);

EtcdNodePanel.this.onFileSaved(target);
}
});
}

private void createNodeActions() {
Expand Down Expand Up @@ -298,6 +326,28 @@ protected void onModalTriggerClick(AjaxRequestTarget target) {
actionModel.setObject(getModelObject());
}
});

breadcrumbAndActions.add(new TriggerModalLink<EtcdNode>("addFile", getModel(),
addFileModalPanel) {
private static final long serialVersionUID = 1L;

@Override
protected void onConfigure() {
super.onConfigure();
if (EtcdNodePanel.this.getModelObject() != null && EtcdNodePanel.this
.getModelObject().isDir()) {
add(AttributeModifier.remove("disabled"));
} else {
add(AttributeAppender.append("disabled", "disabled"));
}
}

@Override
protected void onModalTriggerClick(AjaxRequestTarget target) {
updating.setObject(false);
actionModel.setObject(getModelObject());
}
});
}

private void createBreadcrumb() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package org.github.etcd.viewer.html.pages;

import java.util.Properties;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.feedback.FeedbackMessage;
Expand Down Expand Up @@ -136,9 +137,14 @@ protected void onNodedDeleted(AjaxRequestTarget target) {
@Override
protected void onNodeKeyUpdated(AjaxRequestTarget target) {
super.onNodeKeyUpdated(target);

NavigationPage.this.updatePageTitle(target);
}

@Override
protected void onFileSaved(AjaxRequestTarget target) {
super.onFileSaved(target);
target.add(feedback);
}
});

}
Expand Down