1452: Implement a GUI for StatMan Algorithms Importer

Task-Url: https://support.d4science.org/issues/1452

Added save code

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/statistical-algorithms-importer@121993 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2015-12-18 16:55:57 +00:00
parent ca6e5ffd61
commit 1a70c2788c
5 changed files with 147 additions and 51 deletions

View File

@ -43,7 +43,7 @@ public class CodeEditPanel extends ContentPanel {
private EventBus eventBus;
private AceEditor editor;
private TextButton btnSave;
private TextButton btnAddInput;
private TextButton btnAddOutput;
@ -78,13 +78,11 @@ public class CodeEditPanel extends ContentPanel {
@Override
public void onSelect(SelectEvent event) {
saveCode();
}
});
btnAddInput = new TextButton("Input");
btnAddInput.setIcon(StatAlgoImporterResources.INSTANCE.add16());
btnAddInput.setScale(ButtonScale.SMALL);
@ -94,12 +92,12 @@ public class CodeEditPanel extends ContentPanel {
@Override
public void onSelect(SelectEvent event) {
}
});
btnAddInput.disable();
btnAddOutput = new TextButton("Output");
btnAddOutput.setIcon(StatAlgoImporterResources.INSTANCE.add16());
btnAddOutput.setScale(ButtonScale.SMALL);
@ -109,53 +107,76 @@ public class CodeEditPanel extends ContentPanel {
@Override
public void onSelect(SelectEvent event) {
}
});
btnAddOutput.disable();
ToolBar toolBar = new ToolBar();
toolBar.add(btnSave, new BoxLayoutData(new Margins(0)));
toolBar.add(btnAddInput, new BoxLayoutData(new Margins(0)));
toolBar.add(btnAddOutput, new BoxLayoutData(new Margins(0)));
toolBar.add(btnSave, new BoxLayoutData(new Margins(0)));
toolBar.add(btnAddInput, new BoxLayoutData(new Margins(0)));
toolBar.add(btnAddOutput, new BoxLayoutData(new Margins(0)));
editor = new AceEditor();
VerticalLayoutContainer v = new VerticalLayoutContainer();
v.add(toolBar, new VerticalLayoutData(1, -1, new Margins(0)));
v.add(toolBar, new VerticalLayoutData(1, -1, new Margins(0)));
v.add(editor, new VerticalLayoutData(1, 1, new Margins(0)));
add(v);
editor.startEditor();
editor.setShowPrintMargin(false);
if (project != null && project.getMainCode() != null) {
loadCode();
}
editor.getSelection().addSelectionListener(new AceSelectionListener() {
@Override
public void onChangeSelection(AceSelection selection) {
if(selection!=null&& !selection.isEmpty()){
if (selection != null && !selection.isEmpty()) {
btnAddInput.enable();
btnAddOutput.enable();
} else {
btnAddInput.disable();
btnAddOutput.disable();
}
}
});
}
protected void saveCode() {
String code = editor.getText();
StatAlgoImporterServiceAsync.INSTANCE.saveCode(code,
new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error on save code: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
@Override
public void onSuccess(Void result) {
Log.debug("Code is saved!");
UtilsGXT3.info("Code Saved", "Code is saved!");
}
});
}
private void loadCode() {
@ -210,7 +231,7 @@ public class CodeEditPanel extends ContentPanel {
protected String getSelectedText() {
String selectedText = null;
AceSelection editorSelection = editor.getSelection();
if (!editorSelection.isEmpty()) {
AceEditorCursorPosition startPosition;
@ -245,9 +266,10 @@ public class CodeEditPanel extends ContentPanel {
}
}
} else {
String tempTest;
String tempTest;
tempTest = editor.getLine(startPosition.getRow());
selectedText = tempTest.substring(startPosition.getColumn(), endPosition.getColumn()+1);
selectedText = tempTest.substring(startPosition.getColumn(),
endPosition.getColumn() + 1);
}
}

View File

@ -62,4 +62,6 @@ public interface StatAlgoImporterService extends RemoteService {
public Project openProjectOnWorkspace(ItemDescription newProjectFolder)
throws StatAlgoImporterServiceException;
public void saveCode(String code) throws StatAlgoImporterServiceException;
}

View File

@ -53,4 +53,6 @@ public interface StatAlgoImporterServiceAsync {
void openProjectOnWorkspace(ItemDescription newProjectFolder,
AsyncCallback<Project> asyncCallback);
void saveCode(String code, AsyncCallback<Void> asyncCallback);
}

View File

@ -167,19 +167,19 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
@Override
public Project openProjectOnWorkspace(ItemDescription newProjectFolder)
throws StatAlgoImporterServiceException {
try {
HttpSession session = this.getThreadLocalRequest().getSession();
ASLSession aslSession=SessionUtil.getAslSession(session);
ASLSession aslSession = SessionUtil.getAslSession(session);
logger.debug("openProjectOnWorkspace()");
Project project=ProjectArchiver.readProject(newProjectFolder,aslSession);
Project project = ProjectArchiver.readProject(newProjectFolder,
aslSession);
SessionUtil.setProjectSession(session, project);
return project;
} catch (StatAlgoImporterServiceException e) {
e.printStackTrace();
@ -190,9 +190,8 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
e.printStackTrace();
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
@Override
public void setMainCode(ItemDescription itemDescription)
@ -228,7 +227,7 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
HttpSession session = this.getThreadLocalRequest().getSession();
ASLSession aslSession = SessionUtil.getAslSession(session);
logger.debug("addResourceToProject(): " + itemDescription);
if (itemDescription == null || itemDescription.getId()==null) {
if (itemDescription == null || itemDescription.getId() == null) {
throw new StatAlgoImporterServiceException(
"Add resource to project is failed, invalid resource: "
+ itemDescription);
@ -257,7 +256,7 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
}
}
@Override
public void deleteResourceOnProject(ItemDescription itemDescription)
throws StatAlgoImporterServiceException {
@ -265,7 +264,7 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
HttpSession session = this.getThreadLocalRequest().getSession();
ASLSession aslSession = SessionUtil.getAslSession(session);
logger.debug("deleteResourceOnProject(): " + itemDescription);
if (itemDescription == null || itemDescription.getId()==null) {
if (itemDescription == null || itemDescription.getId() == null) {
throw new StatAlgoImporterServiceException(
"Delete resource on project is failed, invalid resource: "
+ itemDescription);
@ -285,12 +284,12 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
e.printStackTrace();
throw e;
} catch (Throwable e) {
logger.error("deleteResourceOnProject(): " + e.getLocalizedMessage(),
e);
logger.error(
"deleteResourceOnProject(): " + e.getLocalizedMessage(), e);
e.printStackTrace();
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
@Override
@ -298,8 +297,8 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
throws StatAlgoImporterServiceException {
try {
HttpSession session = this.getThreadLocalRequest().getSession();
ASLSession aslSession=SessionUtil.getAslSession(session);
logger.debug("saveProject():"+inputData);
ASLSession aslSession = SessionUtil.getAslSession(session);
logger.debug("saveProject():" + inputData);
Project project = SessionUtil.getProjectSession(session);
if (project != null) {
project.setInputData(inputData);
@ -318,10 +317,39 @@ public class StatAlgoImporterServiceImpl extends RemoteServiceServlet implements
e.printStackTrace();
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
@Override
public void saveCode(String code) throws StatAlgoImporterServiceException {
try {
HttpSession session = this.getThreadLocalRequest().getSession();
ASLSession aslSession = SessionUtil.getAslSession(session);
logger.debug("saveCode():" + code);
Project project = SessionUtil.getProjectSession(session);
if (project != null) {
MainCode mainCode = project.getMainCode();
if (mainCode == null|| mainCode.getItemDescription()==null) {
throw new StatAlgoImporterServiceException("No main code set!");
} else {
FilesStorage filesStorage = new FilesStorage();
filesStorage.saveItem(aslSession.getUsername(), mainCode
.getItemDescription().getId(), code);
}
} else {
throw new StatAlgoImporterServiceException("No project open!");
}
return;
} catch (StatAlgoImporterServiceException e) {
e.printStackTrace();
throw e;
} catch (Throwable e) {
logger.error("saveCode(): " + e.getLocalizedMessage(), e);
e.printStackTrace();
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
}

View File

@ -1,5 +1,6 @@
package org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URLConnection;
@ -208,5 +209,46 @@ public class FilesStorage {
return is;
}
/**
*
* @param user User
* @param itemId Item on workspace
* @param data String to save
* @throws StatAlgoImporterServiceException
*/
public void saveItem(String user, String itemId, String data)
throws StatAlgoImporterServiceException {
Workspace ws;
try {
ws = HomeLibrary.getUserWorkspace(user);
WorkspaceItem workSpaceItem = ws.getItem(itemId);
if (workSpaceItem==null) {
throw new StatAlgoImporterServiceException(
"No item retrieved on workspace!");
} else {
if (workSpaceItem.isFolder()) {
throw new StatAlgoImporterServiceException(
"Folder is not valid item!");
} else {
}
}
// convert String into InputStream
InputStream is = new ByteArrayInputStream(data.getBytes());
ws.updateItem(itemId, is);
return;
} catch (WorkspaceFolderNotFoundException | InternalErrorException
| HomeNotFoundException | ItemNotFoundException | InsufficientPrivilegesException | ItemAlreadyExistException | WrongDestinationException e) {
e.printStackTrace();
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
}
}
}