Added lock system server-side in order to avoid changes during ws-thredds sync

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@165491 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-03-28 10:42:50 +00:00
parent 780b017d1f
commit 65643bffc4
1 changed files with 119 additions and 43 deletions

View File

@ -44,6 +44,9 @@ import org.gcube.common.homelibrary.home.workspace.search.SearchItem;
import org.gcube.common.homelibrary.home.workspace.trash.WorkspaceTrashFolder;
import org.gcube.common.homelibrary.home.workspace.trash.WorkspaceTrashItem;
import org.gcube.common.portal.PortalContext;
import org.gcube.portal.wssynclibrary.shared.ItemNotSynched;
import org.gcube.portal.wssynclibrary.shared.WorkspaceFolderLocked;
import org.gcube.portal.wssynclibrary.thredds.WorkspaceThreddsSynchronize;
import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
import org.gcube.portlets.user.workspace.client.interfaces.GXTCategorySmartFolder;
@ -727,6 +730,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
try {
Workspace workspace = getWorkspace();
checkItemLocked(destinationId);
for (String itemId : ids) {
if(itemId == null)
@ -741,6 +746,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
break;
}
checkItemLocked(itemId);
String sourceSharedId = null;
boolean sourceItemIsShared = sourceItem.isShared();
@ -771,13 +778,16 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return Boolean.TRUE;
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
}catch (InsufficientPrivilegesException e) {
workspaceLogger.error("Error in server Item move", e);
String error1 = "An error occurred on moving item. "+e.getMessage();
throw new Exception(error1);
} catch (Exception e) {
workspaceLogger.error("Error in server Item move", e);
workspaceLogger.error("Item move error.", e);
String error2 = ConstantsExplorer.SERVER_ERROR + " moving item. "+e.getMessage();
throw new Exception(error2);
}
@ -942,6 +952,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
if(itemId == null)
throw new Exception(IDENTIFIER_IS_NULL);
checkItemLocked(itemId);
Workspace workspace = getWorkspace();
workspaceLogger.trace("removeItem item for id: "+itemId);
//NOTIFICATION
@ -962,6 +974,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return Boolean.TRUE;
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
} catch (InsufficientPrivilegesException e) {
workspaceLogger.error("Error in server Item remove", e);
String error = "Insufficient Privileges to remove the item";
@ -972,7 +987,7 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
workspaceLogger.error(error, e);
throw new Exception(error);
} catch (Exception e) {
workspaceLogger.error("Error in server Item remove", e);
workspaceLogger.error("Remove item error.", e);
String error = ConstantsExplorer.SERVER_ERROR +" deleting item. "+e.getMessage();
throw new Exception(error);
}
@ -1002,6 +1017,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
if(itemId == null)
throw new Exception(IDENTIFIER_IS_NULL);
checkItemLocked(itemId);
Workspace workspace = getWorkspace();
workspaceLogger.trace("rename item itemId: "+itemId+" old name "+ previousName +", new name: "+newName);
workspace.renameItem(itemId, newName);
@ -1033,6 +1050,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return true;
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
} catch (InsufficientPrivilegesException e) {
String error = "Insufficient Privileges to rename the item";
workspaceLogger.error(error, e);
@ -1077,6 +1097,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
if(nameFolder == null)
throw new Exception("Folder name is null");
checkItemLocked(parent.getIdentifier());
Workspace workspace = getWorkspace();
WorkspaceFolder wsFolder = workspace.createFolder(nameFolder, description, parent.getIdentifier());
WorkspaceItem folderDestinationItem = workspace.getItem(parent.getIdentifier());
@ -1084,6 +1106,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
GWTWorkspaceBuilder builder = getGWTWorkspaceBuilder();
return builder.buildGXTFolderModelItem(wsFolder, parent);
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
} catch(InsufficientPrivilegesException e){
String error = "Insufficient Privileges to create the folder";
workspaceLogger.error(error, e);
@ -1550,6 +1575,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
throw new Exception("Parent ID is null");
}
checkItemLocked(parentId);
workspaceLogger.trace("create url in parent id: "+parentId);
ExternalUrl ext = workspace.createExternalUrl(name, description, url, parentId);
WorkspaceItem parent = workspace.getItem(parentId); //get item from workspace
@ -1559,6 +1586,10 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
FolderModel parentFileModel = builder.buildGXTFolderModelItem((WorkspaceFolder) parent, null);
return builder.buildGXTFileModelItem(ext, parentFileModel);
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
} catch (Exception e) {
workspaceLogger.error("Error in server create url in parent id ", e);
// workspaceLogger.trace("Error in server create url in parent id " + e);
@ -1781,6 +1812,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
try {
checkItemLocked(destinationFolderId);
Workspace workspace = getWorkspace();
WorkspaceItem sourceItem = workspace.getItem(itemId); //GET SOURCE ITEM BEFORE COPY
String sourceSharedId = sourceItem.getIdSharedFolder();
@ -1793,6 +1826,10 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return true;
return false;
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
}catch (InsufficientPrivilegesException e) {
String error = "An error occurred on copying item, " +e.getMessage() + ". "+ConstantsExplorer.TRY_AGAIN;
throw new Exception(error);
@ -1827,6 +1864,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
try {
checkItemLocked(destinationFolderId);
Workspace workspace = getWorkspace();
boolean error = false;
@ -1850,6 +1889,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return true; //copied is true
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
}catch (InsufficientPrivilegesException e) {
String error = "An error occurred on copying item, " +e.getMessage() + ". "+ConstantsExplorer.TRY_AGAIN;
throw new Exception(error);
@ -1910,6 +1952,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
try {
checkItemLocked(folder.getIdentifier());
Workspace workspace = getWorkspace();
workspaceLogger.info("shareFolder "+ folder.getIdentifier()
@ -1977,6 +2021,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return created;
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
} catch (InsufficientPrivilegesException e) {
workspaceLogger.error("Error in shareFolder ", e);
String error = "An error occurred on creating shared folder. "+ e.getMessage();
@ -2017,6 +2064,7 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
try {
Workspace workspace = getWorkspace();
WorkspaceItem wsItem = workspace.getItem(folderSharedId);
if(NotificationsUtil.isASharedFolder(wsItem)){
@ -2042,43 +2090,6 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
}
}
/* (non-Javadoc)
* @see org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceService#getListUserSharedBySharedItem(java.lang.String)
*/
/*@Override
public List<InfoContactModel> getListUserSharedBySharedItem(String sharedItemId) throws Exception{
workspaceLogger.trace("Get ListUserSharedBySharedItem "+ sharedItemId);
try {
WorkspaceFolder wsFolder = getSharedWorkspaceFolderForId(sharedItemId);
if(wsFolder!=null){
if(isASharedFolder(wsFolder, true)){
GWTWorkspaceBuilder builder = getGWTWorkspaceBuilder();
WorkspaceSharedFolder wsSharedFolder = (WorkspaceSharedFolder) wsFolder;
List<String> listPortalLogin = wsSharedFolder.getUsers();
workspaceLogger.trace("getListUserSharedByFolderSharedId return "+ listPortalLogin.size() + " user/s");
if(isTestMode())
return builder.buildGxtInfoContactFromPortalLoginTestMode(listPortalLogin);
return builder.buildGxtInfoContactsFromPortalLogins(listPortalLogin);
}else{
workspaceLogger.warn("wsFolder with id: "+sharedItemId +" is not a: "+WorkspaceItemType.SHARED_FOLDER +", returning null");
return null;
}
}
workspaceLogger.warn("wsFolder with id: "+sharedItemId +" is null, returning null");
return null;
} catch (Exception e) {
workspaceLogger.error("Error in getListUserSharedByItemId ", e);
throw new Exception(e.getMessage());
}
}*/
/* (non-Javadoc)
* @see org.gcube.portlets.user.workspace.client.rpc.GWTWorkspaceService#unSharedFolderByFolderSharedId(java.lang.String)
*/
@ -2099,7 +2110,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
workspaceLogger.trace("unSharedFolderByFolderSharedId "+ folderSharedId);
try {
checkItemLocked(folderSharedId);
if(isASharedFolder(folderSharedId, true)){
Workspace workspace = getWorkspace();
@ -2136,6 +2149,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
throw new WorkspaceHandledException(msg);
}
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
} catch (InternalErrorException e) {
workspaceLogger.error("Error in unSharedFolderByFolderSharedId ", e);
String error = "An error occerred on unsharing folder. "+ e.getMessage();
@ -2725,6 +2741,7 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
for (String itemId : ids) {
//NOTIFICATION
WorkspaceItem wsItem = workspace.getItem(itemId);
checkItemLocked(itemId);
//SAVING ATTRIBUTE FOR NOTIFICATION
boolean sourceItemIsShared = wsItem.isShared();
String itemName = wsItem.getName();
@ -2736,10 +2753,10 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
garbage.put(itemId, new GarbageItem(sourceItemIsShared, itemName, itemId, sourceFolderSharedId));
// workspace.removeItem(itemId);
// //IF SOURCE SHARED FOLDER IS NOT NULL
// if(sourceFolderSharedId!=null)
// NotificationsUtil.checkSendNotifyRemoveItemToShare(this.getThreadLocalRequest().getSession(), sourceItemIsShared, itemName, itemId, sourceFolderSharedId);
//workspace.removeItem(itemId);
////IF SOURCE SHARED FOLDER IS NOT NULL
//if(sourceFolderSharedId!=null)
//NotificationsUtil.checkSendNotifyRemoveItemToShare(this.getThreadLocalRequest().getSession(), sourceItemIsShared, itemName, itemId, sourceFolderSharedId);
}
//ITEM ID - ERROR
@ -2767,6 +2784,9 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return frontEndError;
}catch (WorkspaceFolderLocked e1){
throw new Exception(e1.getMessage());
} catch (InsufficientPrivilegesException e) {
workspaceLogger.error("Error in server Item remove", e);
String error = "An error occurred on deleting item. "+e.getMessage();
@ -4281,4 +4301,60 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
}
}
/**
* Check item locked.
*
* @param itemId the item id
* @return true, if successful
* @throws WorkspaceFolderLocked the workspace folder locked
* @throws Exception the exception
*/
private boolean checkItemLocked(String itemId) throws WorkspaceFolderLocked, Exception{
if(itemId==null || itemId.isEmpty())
throw new Exception(IDENTIFIER_IS_NULL);
Workspace ws;
WorkspaceItem workItem = null;
try{
ws = getWorkspace();
workItem = ws.getItem(itemId);
//IF THE ITEM IS A FOLDER, CHECKING IT
if(workItem.isFolder())
WorkspaceThreddsSynchronize.getInstance().checkItemSynched(workItem.getId());
else{
//IF THE ITEM IS A FILE, CHECKING ITS PARENT
WorkspaceFolder parent = workItem.getParent();
if(parent!=null){
WorkspaceThreddsSynchronize.getInstance().checkItemSynched(parent.getId());
}
}
//in this case the folder is synched but not locked
return false;
}catch(ItemNotSynched e1){
//in this case the folder is not synched;
return false;
}catch(WorkspaceFolderLocked e2){
//in this case the folder synching is on-going and the folder is locked;
String msg = "The folder";
msg += workItem!=null?": "+workItem.getName():"";
msg += " is locked by a sync. You can not change its content";
workspaceLogger.warn(msg, e2);
throw new WorkspaceFolderLocked(itemId, msg);
}catch(InternalErrorException | ItemNotFoundException | HomeNotFoundException | WorkspaceFolderNotFoundException e){
workspaceLogger.warn(e);
throw new Exception("Sorry an error occurred during checking is folder locked, Refresh and try again");
}
}
}