2012-05-29 22:36:32 -03:00
/ *
2020-11-07 11:56:38 -08:00
* Copyright [ 2015 ] [ wisemapping ]
*
* Licensed under WiseMapping Public License , Version 1 . 0 ( the " License " ) .
* It is basically the Apache License , Version 2 . 0 ( the " License " ) plus the
* " powered by wisemapping " text requirement on every single page ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the license at
*
* http : //www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2012-05-29 22:36:32 -03:00
2012-02-12 21:57:11 -03:00
package com.wisemapping.rest ;
2021-12-24 18:03:23 -08:00
import com.wisemapping.exceptions.* ;
import com.wisemapping.model.* ;
import com.wisemapping.rest.model.* ;
2012-02-21 14:22:43 -03:00
import com.wisemapping.security.Utils ;
2021-12-24 18:03:23 -08:00
import com.wisemapping.service.* ;
2012-04-07 12:45:35 -03:00
import com.wisemapping.validator.MapInfoValidator ;
2020-07-29 12:30:44 -07:00
import org.apache.log4j.Logger ;
2012-03-14 01:49:05 -03:00
import org.jetbrains.annotations.NotNull ;
2012-02-16 01:16:51 -03:00
import org.springframework.beans.factory.annotation.Autowired ;
2012-06-09 22:49:54 -03:00
import org.springframework.beans.factory.annotation.Qualifier ;
2012-02-21 20:04:17 -03:00
import org.springframework.http.HttpStatus ;
2012-02-12 21:57:11 -03:00
import org.springframework.stereotype.Controller ;
2012-04-07 12:45:35 -03:00
import org.springframework.validation.BeanPropertyBindingResult ;
import org.springframework.validation.BindingResult ;
2021-12-24 18:03:23 -08:00
import org.springframework.web.bind.annotation.* ;
2012-02-16 01:16:51 -03:00
import org.springframework.web.servlet.ModelAndView ;
2012-02-12 21:57:11 -03:00
2012-03-14 01:49:05 -03:00
import javax.servlet.http.HttpServletResponse ;
2012-06-03 19:19:48 -03:00
import java.io.ByteArrayInputStream ;
2012-02-12 21:57:11 -03:00
import java.io.IOException ;
2020-11-07 11:56:38 -08:00
import java.nio.charset.StandardCharsets ;
2021-12-24 18:03:23 -08:00
import java.util.* ;
import java.util.stream.Collectors ;
2012-02-12 21:57:11 -03:00
2012-03-14 22:10:44 -03:00
2012-02-12 21:57:11 -03:00
@Controller
2012-03-13 15:57:30 -03:00
public class MindmapController extends BaseController {
2021-12-24 18:03:23 -08:00
final Logger logger = Logger . getLogger ( MindmapController . class ) ;
2012-09-30 17:15:01 -03:00
2020-11-06 21:35:54 -08:00
private static final String LATEST_HISTORY_REVISION = " latest " ;
2012-09-30 17:15:01 -03:00
2012-06-09 22:49:54 -03:00
@Qualifier ( " mindmapService " )
2012-02-16 01:16:51 -03:00
@Autowired
2012-02-12 21:57:11 -03:00
private MindmapService mindmapService ;
2014-02-11 02:04:09 -03:00
@Qualifier ( " labelService " )
@Autowired
private LabelService labelService ;
2012-06-03 11:16:38 -03:00
@RequestMapping ( method = RequestMethod . GET , value = " /maps/{id} " , produces = { " application/json " , " application/xml " , " text/html " } )
2012-02-12 21:57:11 -03:00
@ResponseBody
2014-01-16 20:09:22 -03:00
public RestMindmap retrieve ( @PathVariable int id ) throws WiseMappingException {
2012-06-09 22:49:54 -03:00
final User user = Utils . getUser ( ) ;
2013-03-29 21:09:28 -03:00
final Mindmap mindMap = findMindmapById ( id ) ;
2014-01-16 20:09:22 -03:00
return new RestMindmap ( mindMap , user ) ;
2012-02-12 21:57:11 -03:00
}
2012-02-17 10:42:20 -03:00
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . GET , value = " /maps/ " , produces = { " application/json " , " application/xml " } )
2014-03-09 22:08:46 -03:00
public RestMindmapList retrieveList ( @RequestParam ( required = false ) String q ) throws IOException {
2012-06-09 22:49:54 -03:00
final User user = Utils . getUser ( ) ;
2012-02-17 10:42:20 -03:00
2014-03-09 22:08:46 -03:00
final MindmapFilter filter = MindmapFilter . parse ( q ) ;
2012-06-17 21:43:34 -03:00
final List < Collaboration > collaborations = mindmapService . findCollaborations ( user ) ;
2012-05-23 21:54:03 -03:00
2012-08-15 21:28:51 -03:00
final List < Mindmap > mindmaps = new ArrayList < Mindmap > ( ) ;
2012-06-09 22:49:54 -03:00
for ( Collaboration collaboration : collaborations ) {
2012-08-15 21:28:51 -03:00
final Mindmap mindmap = collaboration . getMindMap ( ) ;
2012-05-23 21:54:03 -03:00
if ( filter . accept ( mindmap , user ) ) {
mindmaps . add ( mindmap ) ;
}
2012-02-20 14:42:07 -03:00
}
2014-01-16 20:09:22 -03:00
return new RestMindmapList ( mindmaps , user ) ;
2012-02-17 10:42:20 -03:00
}
2012-02-21 14:22:43 -03:00
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . GET , value = " /maps/{id}/history " , produces = { " application/json " , " application/xml " } )
2014-01-16 20:09:22 -03:00
public RestMindmapHistoryList retrieveHistory ( @PathVariable int id ) throws IOException {
2012-06-17 19:16:39 -03:00
final List < MindMapHistory > histories = mindmapService . findMindmapHistory ( id ) ;
2012-06-17 02:51:01 -03:00
final RestMindmapHistoryList result = new RestMindmapHistoryList ( ) ;
for ( MindMapHistory history : histories ) {
result . addHistory ( new RestMindmapHistory ( history ) ) ;
2012-06-09 22:49:54 -03:00
}
2014-01-16 20:09:22 -03:00
return result ;
2012-06-07 19:45:22 -03:00
}
2015-04-10 00:07:40 -03:00
@RequestMapping ( value = " /maps/{id}/history/{hid} " , method = RequestMethod . POST )
2012-06-17 19:16:39 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2013-02-13 22:51:55 -03:00
public void updateRevertMindmap ( @PathVariable int id , @PathVariable String hid ) throws WiseMappingException , IOException {
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-07-04 02:15:55 -03:00
final User user = Utils . getUser ( ) ;
if ( LATEST_HISTORY_REVISION . equals ( hid ) ) {
// Revert to the latest stored version ...
List < MindMapHistory > mindmapHistory = mindmapService . findMindmapHistory ( id ) ;
if ( mindmapHistory . size ( ) > 0 ) {
final MindMapHistory mindMapHistory = mindmapHistory . get ( 0 ) ;
2013-03-29 14:51:21 -03:00
mindmap . setZippedXml ( mindMapHistory . getZippedXml ( ) ) ;
2012-11-17 21:28:23 -03:00
saveMindmapDocument ( true , mindmap , user ) ;
2012-07-04 02:15:55 -03:00
}
} else {
mindmapService . revertChange ( mindmap , Integer . parseInt ( hid ) ) ;
}
2012-06-17 19:16:39 -03:00
}
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id}/document " , consumes = { " application/xml " , " application/json " } , produces = { " application/json " , " application/xml " } )
2012-09-30 17:15:01 -03:00
@ResponseBody
2014-01-25 04:21:59 -03:00
public Long updateDocument ( @RequestBody RestMindmap restMindmap , @PathVariable int id , @RequestParam ( required = false ) boolean minor , @RequestParam ( required = false ) Long timestamp , @RequestParam ( required = false ) Long session ) throws WiseMappingException , IOException {
2012-02-21 14:22:43 -03:00
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-02-21 14:22:43 -03:00
final User user = Utils . getUser ( ) ;
2012-03-14 01:49:05 -03:00
// Validate arguments ...
2012-02-21 14:22:43 -03:00
final String properties = restMindmap . getProperties ( ) ;
2012-03-14 01:49:05 -03:00
if ( properties = = null ) {
throw new IllegalArgumentException ( " Map properties can not be null " ) ;
}
2012-06-17 21:43:34 -03:00
2012-10-04 20:28:59 -03:00
// Could the map be updated ?
2014-01-25 03:13:51 -03:00
if ( session ! = null ) {
verifyLock ( mindmap , user , session , timestamp ) ;
}
2012-09-30 17:15:01 -03:00
2012-06-17 21:43:34 -03:00
// Update collaboration properties ...
2012-06-17 23:21:02 -03:00
final CollaborationProperties collaborationProperties = mindmap . findCollaborationProperties ( user ) ;
2012-06-17 21:43:34 -03:00
collaborationProperties . setMindmapProperties ( properties ) ;
2012-02-21 14:22:43 -03:00
2012-03-14 01:49:05 -03:00
// Validate content ...
2013-02-22 20:39:51 -03:00
String xml = restMindmap . getXml ( ) ;
2012-03-14 01:49:05 -03:00
if ( xml = = null ) {
throw new IllegalArgumentException ( " Map xml can not be null " ) ;
}
2012-06-17 21:43:34 -03:00
mindmap . setXmlStr ( xml ) ;
2012-03-14 01:49:05 -03:00
// Update map ...
2012-11-17 21:28:23 -03:00
saveMindmapDocument ( minor , mindmap , user ) ;
2012-09-30 17:15:01 -03:00
2012-10-04 20:28:59 -03:00
// Update edition timeout ...
final LockManager lockManager = mindmapService . getLockManager ( ) ;
2014-01-25 03:13:51 -03:00
long result = - 1 ;
if ( session ! = null ) {
final LockInfo lockInfo = lockManager . updateExpirationTimeout ( mindmap , user ) ;
result = lockInfo . getTimestamp ( ) ;
}
return result ;
2012-10-04 20:28:59 -03:00
}
2020-11-07 11:56:38 -08:00
@RequestMapping ( method = RequestMethod . GET , value = { " /maps/{id}/document/xml " , " /maps/{id}/document/xml-pub " } , consumes = { " text/plain " } , produces = { " application/xml; charset=UTF-8 " } )
2013-01-31 22:50:21 -03:00
@ResponseBody
2013-02-03 12:51:12 -03:00
public byte [ ] retrieveDocument ( @PathVariable int id , @NotNull HttpServletResponse response ) throws WiseMappingException , IOException {
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2013-03-14 21:38:38 -03:00
String xmlStr = mindmap . getXmlStr ( ) ;
2020-11-07 11:56:38 -08:00
return xmlStr . getBytes ( StandardCharsets . UTF_8 ) ;
2013-01-31 22:50:21 -03:00
}
2014-01-25 04:21:59 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = { " /maps/{id}/document/xml " } , consumes = { " text/plain " } )
@ResponseBody
public void updateDocument ( @PathVariable int id , @RequestBody String xmlDoc ) throws WiseMappingException , IOException {
final Mindmap mindmap = findMindmapById ( id ) ;
final User user = Utils . getUser ( ) ;
if ( xmlDoc ! = null & & ! xmlDoc . isEmpty ( ) ) {
mindmap . setXmlStr ( xmlDoc ) ;
}
mindmap . setXmlStr ( xmlDoc ) ;
saveMindmapDocument ( false , mindmap , user ) ;
}
2020-11-07 11:56:38 -08:00
@RequestMapping ( method = RequestMethod . GET , value = { " /maps/{id}/{hid}/document/xml " } , consumes = { " text/plain " } , produces = { " application/xml; charset=UTF-8 " } )
2013-02-13 22:46:45 -03:00
@ResponseBody
2013-03-31 15:33:00 -03:00
public byte [ ] retrieveDocument ( @PathVariable int id , @PathVariable int hid , @NotNull HttpServletResponse response ) throws WiseMappingException , IOException {
2013-02-13 22:46:45 -03:00
final MindMapHistory mindmapHistory = mindmapService . findMindmapHistory ( id , hid ) ;
2013-02-13 22:51:55 -03:00
return mindmapHistory . getUnzipXml ( ) ;
2013-02-13 22:46:45 -03:00
}
2012-10-04 20:48:01 -03:00
private void verifyLock ( @NotNull Mindmap mindmap , @NotNull User user , long session , long timestamp ) throws WiseMappingException {
2012-11-10 17:19:28 -03:00
// The lock was lost, reclaim as the ownership of it.
final LockManager lockManager = mindmapService . getLockManager ( ) ;
final boolean lockLost = lockManager . isLocked ( mindmap ) ;
if ( ! lockLost ) {
lockManager . lock ( mindmap , user , session ) ;
}
final LockInfo lockInfo = lockManager . getLockInfo ( mindmap ) ;
2012-11-14 20:33:42 -03:00
if ( lockInfo . getUser ( ) . identityEquality ( user ) ) {
2020-07-29 12:30:44 -07:00
long savedTimestamp = mindmap . getLastModificationTime ( ) . getTimeInMillis ( ) ;
final boolean outdated = savedTimestamp > timestamp ;
2012-11-10 17:19:28 -03:00
if ( lockInfo . getSession ( ) = = session ) {
// Timestamp might not be returned to the client. This try to cover this case, ignoring the client timestamp check.
final User lastEditor = mindmap . getLastEditor ( ) ;
2012-11-16 22:37:29 -03:00
boolean editedBySameUser = lastEditor = = null | | user . identityEquality ( lastEditor ) ;
if ( outdated & & ! editedBySameUser ) {
2020-07-29 12:30:44 -07:00
throw new SessionExpiredException ( " Map has been updated by " + ( lastEditor . getEmail ( ) ) + " ,Timestamp: " + timestamp + " , " + savedTimestamp + " , User: " + lastEditor . getId ( ) + " : " + user . getId ( ) + " ,Mail:' " + lastEditor . getEmail ( ) + " ':' " + user . getEmail ( ) , lastEditor ) ;
2012-11-10 17:19:28 -03:00
}
} else if ( outdated ) {
2020-07-29 12:30:44 -07:00
logger . warn ( " Sessions: " + session + " : " + lockInfo . getSession ( ) + " ,Timestamp: " + timestamp + " : " + savedTimestamp ) ;
// @Todo: Temporally disabled to unblock save action. More research needed.
// throw new MultipleSessionsOpenException("Sessions:" + session + ":" + lockInfo.getSession() + ",Timestamp: " + timestamp + ": " + savedTimestamp);
2012-11-10 17:19:28 -03:00
}
} else {
2012-11-16 22:37:29 -03:00
throw new SessionExpiredException ( " Different Users. " , lockInfo . getUser ( ) ) ;
2012-11-10 17:19:28 -03:00
}
2012-02-21 14:22:43 -03:00
}
2012-02-21 20:04:17 -03:00
2012-05-23 20:05:16 -03:00
/ * *
* The intention of this method is the update of several properties at once . . .
* /
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id} " , consumes = { " application/xml " , " application/json " } , produces = { " application/json " , " application/xml " } )
2012-05-23 20:05:16 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2014-01-16 20:09:22 -03:00
public void updateProperties ( @RequestBody RestMindmap restMindmap , @PathVariable int id , @RequestParam ( required = false ) boolean minor ) throws IOException , WiseMappingException {
2012-05-23 20:05:16 -03:00
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-05-23 20:05:16 -03:00
final User user = Utils . getUser ( ) ;
final String xml = restMindmap . getXml ( ) ;
2013-03-31 15:33:00 -03:00
if ( xml ! = null & & ! xml . isEmpty ( ) ) {
2012-06-17 21:43:34 -03:00
mindmap . setXmlStr ( xml ) ;
2012-05-23 20:05:16 -03:00
}
// Update title ...
final String title = restMindmap . getTitle ( ) ;
2012-06-17 21:43:34 -03:00
if ( title ! = null & & ! title . equals ( mindmap . getTitle ( ) ) ) {
2012-05-23 20:05:16 -03:00
if ( mindmapService . getMindmapByTitle ( title , user ) ! = null ) {
throw buildValidationException ( " title " , " You already have a map with this title " ) ;
}
2012-06-17 21:43:34 -03:00
mindmap . setTitle ( title ) ;
2012-05-23 20:05:16 -03:00
}
// Update description ...
final String description = restMindmap . getDescription ( ) ;
if ( description ! = null ) {
2012-06-17 21:43:34 -03:00
mindmap . setDescription ( description ) ;
2012-05-23 20:05:16 -03:00
}
final String tags = restMindmap . getTags ( ) ;
if ( tags ! = null ) {
2012-06-17 21:43:34 -03:00
mindmap . setTags ( tags ) ;
}
// Update document properties ...
final String properties = restMindmap . getProperties ( ) ;
if ( properties ! = null ) {
2012-06-17 23:21:02 -03:00
final CollaborationProperties collaborationProperties = mindmap . findCollaborationProperties ( user ) ;
2012-06-17 21:43:34 -03:00
collaborationProperties . setMindmapProperties ( properties ) ;
2012-05-23 20:05:16 -03:00
}
// Update map ...
2012-11-17 21:28:23 -03:00
saveMindmapDocument ( minor , mindmap , user ) ;
2012-05-23 20:05:16 -03:00
}
2013-03-29 21:09:28 -03:00
@NotNull
private Mindmap findMindmapById ( int id ) throws MapCouldNotFoundException {
Mindmap result = mindmapService . findMindmapById ( id ) ;
2013-03-31 15:33:00 -03:00
if ( result = = null ) {
2013-03-29 21:09:28 -03:00
throw new MapCouldNotFoundException ( " Map could not be found. Id: " + id ) ;
}
return result ;
}
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id}/title " , consumes = { " text/plain " } , produces = { " application/json " , " application/xml " } )
2012-03-15 01:47:42 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2012-06-03 11:16:38 -03:00
public void updateTitle ( @RequestBody String title , @PathVariable int id ) throws WiseMappingException {
2012-03-15 01:47:42 -03:00
2013-03-29 21:09:28 -03:00
final Mindmap mindMap = findMindmapById ( id ) ;
2012-03-15 01:47:42 -03:00
final User user = Utils . getUser ( ) ;
// Is there a map with the same name ?
if ( mindmapService . getMindmapByTitle ( title , user ) ! = null ) {
2012-05-23 20:05:16 -03:00
throw buildValidationException ( " title " , " You already have a mindmap with this title " ) ;
2012-03-15 01:47:42 -03:00
}
// Update map ...
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-03-15 01:47:42 -03:00
mindmap . setTitle ( title ) ;
2020-11-07 11:56:38 -08:00
mindmapService . updateMindmap ( mindMap , false ) ;
2012-03-15 01:47:42 -03:00
}
2021-12-24 18:03:23 -08:00
@RequestMapping ( method = RequestMethod . POST , value = " /maps/{id}/collabs/ " , consumes = { " application/json " , " application/xml " } , produces = { " application/json " , " application/xml " } )
2012-06-09 15:49:19 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2013-03-29 21:09:28 -03:00
public void updateCollabs ( @PathVariable int id , @NotNull @RequestBody RestCollaborationList restCollabs ) throws CollaborationException , MapCouldNotFoundException {
final Mindmap mindMap = findMindmapById ( id ) ;
2012-06-12 11:23:47 -03:00
// Only owner can change collaborators...
2012-06-09 15:49:19 -03:00
final User user = Utils . getUser ( ) ;
2012-06-12 11:23:47 -03:00
if ( ! mindMap . hasPermissions ( user , CollaborationRole . OWNER ) ) {
2012-06-09 22:49:54 -03:00
throw new IllegalArgumentException ( " No enough permissions " ) ;
}
// Compare one by one if some of the elements has been changed ....
final Set < Collaboration > collabsToRemove = new HashSet < Collaboration > ( mindMap . getCollaborations ( ) ) ;
for ( RestCollaboration restCollab : restCollabs . getCollaborations ( ) ) {
2012-06-09 22:55:55 -03:00
final Collaboration collaboration = mindMap . findCollaboration ( restCollab . getEmail ( ) ) ;
// Validate role format ...
String roleStr = restCollab . getRole ( ) ;
if ( roleStr = = null ) {
throw new IllegalArgumentException ( roleStr + " is not a valid role " ) ;
}
2012-06-09 22:49:54 -03:00
2015-04-11 16:09:29 -03:00
// Remove from the list of pendings to remove ...
if ( collaboration ! = null ) {
collabsToRemove . remove ( collaboration ) ;
}
2012-06-09 22:55:55 -03:00
// Is owner ?
final CollaborationRole role = CollaborationRole . valueOf ( roleStr . toUpperCase ( ) ) ;
if ( role ! = CollaborationRole . OWNER ) {
2012-06-16 11:37:40 -03:00
mindmapService . addCollaboration ( mindMap , restCollab . getEmail ( ) , role , restCollabs . getMessage ( ) ) ;
2012-06-09 22:49:54 -03:00
}
}
// Remove all collaborations that no applies anymore ..
for ( final Collaboration collaboration : collabsToRemove ) {
2012-06-13 23:04:29 -03:00
mindmapService . removeCollaboration ( mindMap , collaboration ) ;
2012-06-09 22:49:54 -03:00
}
2012-06-09 15:49:19 -03:00
}
2021-12-24 18:03:23 -08:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id}/collabs/ " , consumes = { " application/json " , " application/xml " } , produces = { " application/json " , " application/xml " } )
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
public void addCollab ( @PathVariable int id , @NotNull @RequestBody RestCollaborationList restCollabs ) throws CollaborationException , MapCouldNotFoundException {
final Mindmap mindMap = findMindmapById ( id ) ;
// Only owner can change collaborators...
final User user = Utils . getUser ( ) ;
if ( ! mindMap . hasPermissions ( user , CollaborationRole . OWNER ) ) {
throw new IllegalArgumentException ( " No enough permissions " ) ;
}
// Has any role changed ?. Just removed it.
final Map < String , Collaboration > mapsByEmail = mindMap
. getCollaborations ( )
. stream ( )
. collect ( Collectors . toMap ( collaboration - > collaboration . getCollaborator ( ) . getEmail ( ) , collaboration - > collaboration ) ) ;
restCollabs
. getCollaborations ( )
. forEach ( collab - > {
final String email = collab . getEmail ( ) ;
if ( mapsByEmail . containsKey ( email ) ) {
try {
mindmapService . removeCollaboration ( mindMap , mapsByEmail . get ( email ) ) ;
} catch ( CollaborationException e ) {
logger . error ( e ) ;
}
}
} ) ;
// Great, let's add all the collabs again ...
for ( RestCollaboration restCollab : restCollabs . getCollaborations ( ) ) {
final Collaboration collaboration = mindMap . findCollaboration ( restCollab . getEmail ( ) ) ;
// Validate role format ...
String roleStr = restCollab . getRole ( ) ;
if ( roleStr = = null ) {
throw new IllegalArgumentException ( roleStr + " is not a valid role " ) ;
}
// Is owner ?
final CollaborationRole role = CollaborationRole . valueOf ( roleStr . toUpperCase ( ) ) ;
if ( role = = CollaborationRole . OWNER ) {
throw new IllegalArgumentException ( " Owner can not be added as part of the collaboration list. " ) ;
}
mindmapService . addCollaboration ( mindMap , restCollab . getEmail ( ) , role , restCollabs . getMessage ( ) ) ;
}
}
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . GET , value = " /maps/{id}/collabs " , produces = { " application/json " , " application/xml " } )
2014-01-16 20:09:22 -03:00
public RestCollaborationList retrieveList ( @PathVariable int id ) throws MapCouldNotFoundException {
2013-03-29 21:09:28 -03:00
final Mindmap mindMap = findMindmapById ( id ) ;
2012-06-09 15:49:19 -03:00
final Set < Collaboration > collaborations = mindMap . getCollaborations ( ) ;
2012-06-09 22:49:54 -03:00
final List < RestCollaboration > collabs = new ArrayList < RestCollaboration > ( ) ;
for ( Collaboration collaboration : collaborations ) {
collabs . add ( new RestCollaboration ( collaboration ) ) ;
}
2014-01-16 20:09:22 -03:00
final RestCollaborationList result = new RestCollaborationList ( ) ;
result . setCollaborations ( collabs ) ;
2012-06-09 15:49:19 -03:00
2014-01-16 20:09:22 -03:00
return result ;
2012-06-09 15:49:19 -03:00
}
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id}/description " , consumes = { " text/plain " } , produces = { " application/json " , " application/xml " } )
2012-03-15 01:47:42 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2012-06-03 11:16:38 -03:00
public void updateDescription ( @RequestBody String description , @PathVariable int id ) throws WiseMappingException {
2012-03-15 01:47:42 -03:00
2013-03-29 21:09:28 -03:00
final Mindmap mindMap = findMindmapById ( id ) ;
2012-03-15 01:47:42 -03:00
final User user = Utils . getUser ( ) ;
// Update map ...
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-03-15 01:47:42 -03:00
mindmap . setDescription ( description ) ;
2020-11-07 11:56:38 -08:00
mindmapService . updateMindmap ( mindMap , false ) ;
2012-03-15 01:47:42 -03:00
}
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id}/publish " , consumes = { " text/plain " } , produces = { " application/json " , " application/xml " } )
2012-05-20 21:46:55 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2012-05-29 22:36:32 -03:00
public void updatePublishState ( @RequestBody String value , @PathVariable int id ) throws WiseMappingException {
2012-05-20 21:46:55 -03:00
2013-03-29 21:09:28 -03:00
final Mindmap mindMap = findMindmapById ( id ) ;
2012-05-20 21:46:55 -03:00
2012-06-12 11:23:47 -03:00
final User user = Utils . getUser ( ) ;
2012-06-18 00:40:42 -03:00
if ( ! mindMap . hasPermissions ( user , CollaborationRole . OWNER ) ) {
2012-05-20 21:46:55 -03:00
throw new IllegalArgumentException ( " No enough to execute this operation " ) ;
}
// Update map status ...
mindMap . setPublic ( Boolean . parseBoolean ( value ) ) ;
2020-11-07 11:56:38 -08:00
mindmapService . updateMindmap ( mindMap , false ) ;
2012-05-20 21:46:55 -03:00
}
2012-03-15 01:21:46 -03:00
2012-09-30 17:15:01 -03:00
@RequestMapping ( method = RequestMethod . DELETE , value = " /maps/{id} " )
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2012-11-17 21:28:23 -03:00
public void deleteMapById ( @PathVariable int id ) throws IOException , WiseMappingException {
2012-09-30 17:15:01 -03:00
final User user = Utils . getUser ( ) ;
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-09-30 17:15:01 -03:00
mindmapService . removeMindmap ( mindmap , user ) ;
}
2021-12-24 18:03:23 -08:00
@RequestMapping ( method = RequestMethod . DELETE , value = " /maps/{id}/collabs " )
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
public void deleteCollabByEmail ( @PathVariable int id , @RequestParam ( required = false ) String email ) throws IOException , WiseMappingException {
logger . debug ( " Deleting permission for email: " + email ) ;
final Mindmap mindmap = findMindmapById ( id ) ;
final User user = Utils . getUser ( ) ;
// Only owner can change collaborators...
if ( ! mindmap . hasPermissions ( user , CollaborationRole . OWNER ) ) {
throw new IllegalArgumentException ( " No enough permissions " ) ;
}
final Collaboration collab = mindmap . findCollaboration ( email ) ;
if ( collab ! = null ) {
CollaborationRole role = collab . getRole ( ) ;
// Owner collab can not be removed ...
if ( role = = CollaborationRole . OWNER ) {
throw new IllegalArgumentException ( " Can not remove owner collab " ) ;
}
mindmapService . removeCollaboration ( mindmap , collab ) ;
}
}
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id}/starred " , consumes = { " text/plain " } , produces = { " application/json " , " application/xml " } )
2012-05-29 22:36:32 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2021-12-24 18:03:23 -08:00
public void updateStarredState ( @RequestBody String value , @PathVariable int id ) throws WiseMappingException {
2012-05-29 22:36:32 -03:00
2021-12-24 18:03:23 -08:00
logger . debug ( " Update starred: " + value ) ;
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-05-29 22:36:32 -03:00
final User user = Utils . getUser ( ) ;
// Update map status ...
2012-06-17 23:21:02 -03:00
final boolean starred = Boolean . parseBoolean ( value ) ;
final Collaboration collaboration = mindmap . findCollaboration ( user ) ;
if ( collaboration = = null ) {
throw new WiseMappingException ( " No enough permissions. " ) ;
}
collaboration . getCollaborationProperties ( ) . setStarred ( starred ) ;
mindmapService . updateCollaboration ( user , collaboration ) ;
2012-05-29 22:36:32 -03:00
}
2014-01-25 03:13:51 -03:00
@RequestMapping ( method = RequestMethod . PUT , value = " /maps/{id}/lock " , consumes = { " text/plain " } , produces = { " application/json " , " application/xml " } )
2012-03-15 01:21:46 -03:00
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2012-09-30 17:15:01 -03:00
public void updateMapLock ( @RequestBody String value , @PathVariable int id ) throws IOException , WiseMappingException {
2012-03-15 01:21:46 -03:00
final User user = Utils . getUser ( ) ;
2012-09-30 17:15:01 -03:00
final LockManager lockManager = mindmapService . getLockManager ( ) ;
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( id ) ;
2012-10-04 20:28:59 -03:00
final boolean lock = Boolean . parseBoolean ( value ) ;
if ( ! lock ) {
lockManager . unlock ( mindmap , user ) ;
} else {
throw new UnsupportedOperationException ( " REST lock must be implemented. " ) ;
}
2012-03-15 01:21:46 -03:00
}
2012-04-06 17:05:42 -03:00
@RequestMapping ( method = RequestMethod . DELETE , value = " /maps/batch " )
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
2021-12-24 18:03:23 -08:00
public void batchDelete ( @RequestParam ( ) String ids ) throws IOException , WiseMappingException {
2012-04-06 17:05:42 -03:00
final User user = Utils . getUser ( ) ;
2021-12-24 18:03:23 -08:00
final String [ ] mapsIds = ids . split ( " , " ) ;
2012-04-06 17:05:42 -03:00
for ( final String mapId : mapsIds ) {
2013-03-29 21:09:28 -03:00
final Mindmap mindmap = findMindmapById ( Integer . parseInt ( mapId ) ) ;
2012-04-06 17:05:42 -03:00
mindmapService . removeMindmap ( mindmap , user ) ;
}
}
2012-06-06 00:48:46 -03:00
@RequestMapping ( method = RequestMethod . POST , value = " /maps " , consumes = { " application/xml " , " application/json " , " application/wisemapping+xml " } )
2012-03-14 01:49:05 -03:00
@ResponseStatus ( value = HttpStatus . CREATED )
2021-12-24 18:03:23 -08:00
public void createMap ( @RequestBody ( required = false ) RestMindmap restMindmap , @NotNull HttpServletResponse response , @RequestParam ( required = false ) String title , @RequestParam ( required = false ) String description ) throws IOException , WiseMappingException {
// If a default maps has not been defined, just create one ...
if ( restMindmap = = null ) {
restMindmap = new RestMindmap ( ) ;
}
2012-06-06 00:48:46 -03:00
// Overwrite title and description if they where specified by parameter.
if ( title ! = null & & ! title . isEmpty ( ) ) {
restMindmap . setTitle ( title ) ;
}
if ( description ! = null & & ! description . isEmpty ( ) ) {
restMindmap . setDescription ( description ) ;
2021-12-24 18:03:23 -08:00
} else {
restMindmap . setDescription ( " " ) ;
2012-06-06 00:48:46 -03:00
}
2012-03-13 15:57:30 -03:00
2012-04-07 12:45:35 -03:00
// Validate ...
final BindingResult result = new BeanPropertyBindingResult ( restMindmap , " " ) ;
new MapInfoValidator ( mindmapService ) . validate ( restMindmap . getDelegated ( ) , result ) ;
if ( result . hasErrors ( ) ) {
throw new ValidationException ( result ) ;
2012-03-14 01:49:05 -03:00
}
// If the user has not specified the xml content, add one ...
2012-08-15 21:28:51 -03:00
final Mindmap delegated = restMindmap . getDelegated ( ) ;
2012-03-14 01:49:05 -03:00
String xml = restMindmap . getXml ( ) ;
if ( xml = = null | | xml . isEmpty ( ) ) {
2012-08-15 21:28:51 -03:00
xml = Mindmap . getDefaultMindmapXml ( restMindmap . getTitle ( ) ) ;
2012-03-14 01:49:05 -03:00
}
2012-03-15 21:13:47 -03:00
delegated . setXmlStr ( xml ) ;
2012-03-14 01:49:05 -03:00
// Add new mindmap ...
2012-06-06 00:48:46 -03:00
final User user = Utils . getUser ( ) ;
2012-03-14 01:49:05 -03:00
mindmapService . addMindmap ( delegated , user ) ;
// Return the new created map ...
response . setHeader ( " Location " , " /service/maps/ " + delegated . getId ( ) ) ;
2012-04-06 20:28:25 -03:00
response . setHeader ( " ResourceId " , Integer . toString ( delegated . getId ( ) ) ) ;
2012-03-13 15:57:30 -03:00
}
2014-01-25 04:21:59 -03:00
@RequestMapping ( method = RequestMethod . POST , value = " /maps/{id} " , consumes = { " application/xml " , " application/json " } , produces = { " application/xml " , " application/json " , " text/plain " } )
2012-03-15 21:13:47 -03:00
@ResponseStatus ( value = HttpStatus . CREATED )
2012-06-03 11:16:38 -03:00
public void createDuplicate ( @RequestBody RestMindmapInfo restMindmap , @PathVariable int id , @NotNull HttpServletResponse response ) throws IOException , WiseMappingException {
2012-05-20 21:46:55 -03:00
// Validate ...
2012-04-07 20:05:50 -03:00
final BindingResult result = new BeanPropertyBindingResult ( restMindmap , " " ) ;
new MapInfoValidator ( mindmapService ) . validate ( restMindmap . getDelegated ( ) , result ) ;
if ( result . hasErrors ( ) ) {
throw new ValidationException ( result ) ;
2012-03-15 21:13:47 -03:00
}
// Some basic validations ...
final User user = Utils . getUser ( ) ;
// Create a shallowCopy of the map ...
2013-03-29 21:09:28 -03:00
final Mindmap mindMap = findMindmapById ( id ) ;
2012-08-15 21:28:51 -03:00
final Mindmap clonedMap = mindMap . shallowClone ( ) ;
2012-03-15 21:13:47 -03:00
clonedMap . setTitle ( restMindmap . getTitle ( ) ) ;
clonedMap . setDescription ( restMindmap . getDescription ( ) ) ;
// Add new mindmap ...
mindmapService . addMindmap ( clonedMap , user ) ;
// Return the new created map ...
response . setHeader ( " Location " , " /service/maps/ " + clonedMap . getId ( ) ) ;
2012-04-07 12:45:35 -03:00
response . setHeader ( " ResourceId " , Integer . toString ( clonedMap . getId ( ) ) ) ;
2012-03-15 21:13:47 -03:00
}
2012-06-06 00:48:46 -03:00
2012-11-17 21:28:23 -03:00
private void saveMindmapDocument ( boolean minor , @NotNull final Mindmap mindMap , @NotNull final User user ) throws WiseMappingException {
2012-06-06 00:48:46 -03:00
final Calendar now = Calendar . getInstance ( ) ;
mindMap . setLastModificationTime ( now ) ;
2012-07-15 00:57:44 -03:00
mindMap . setLastEditor ( user ) ;
2012-06-17 02:51:01 -03:00
mindmapService . updateMindmap ( mindMap , ! minor ) ;
2012-06-06 00:48:46 -03:00
}
2012-06-17 23:21:02 -03:00
private ValidationException buildValidationException ( @NotNull String fieldName , @NotNull String message ) throws WiseMappingException {
2012-06-06 00:48:46 -03:00
final BindingResult result = new BeanPropertyBindingResult ( new RestMindmap ( ) , " " ) ;
result . rejectValue ( fieldName , " error.not-specified " , null , message ) ;
return new ValidationException ( result ) ;
}
2014-02-05 02:11:23 -03:00
@RequestMapping ( method = RequestMethod . DELETE , value = " /labels/maps/{id} " )
@ResponseStatus ( value = HttpStatus . NO_CONTENT )
public void removeLabel ( @RequestBody RestLabel restLabel , @PathVariable int id ) throws WiseMappingException {
final Mindmap mindmap = findMindmapById ( id ) ;
final User currentUser = Utils . getUser ( ) ;
final Label delegated = restLabel . getDelegated ( ) ;
assert currentUser ! = null ;
delegated . setCreator ( currentUser ) ;
mindmapService . removeLabel ( mindmap , delegated ) ;
}
2014-02-25 22:19:52 -03:00
@RequestMapping ( method = RequestMethod . POST , value = " /labels/maps " , consumes = { " application/xml " , " application/json " } )
2014-02-05 21:44:30 -03:00
@ResponseStatus ( value = HttpStatus . OK )
2014-02-05 02:11:23 -03:00
public void addLabel ( @RequestBody RestLabel restLabel , @RequestParam ( required = true ) String ids ) throws WiseMappingException {
int labelId = restLabel . getId ( ) ;
2014-02-11 02:04:09 -03:00
final User user = Utils . getUser ( ) ;
final Label delegated = restLabel . getDelegated ( ) ;
delegated . setCreator ( user ) ;
2020-11-07 11:56:38 -08:00
2014-02-11 02:04:09 -03:00
final Label found = labelService . getLabelById ( labelId , user ) ;
if ( found = = null ) {
throw new LabelCouldNotFoundException ( " Label could not be found. Id: " + labelId ) ;
}
2020-11-23 18:17:48 -08:00
for ( String id : ids . split ( " , " ) ) {
2014-02-05 02:11:23 -03:00
final int mindmapId = Integer . parseInt ( id ) ;
final Mindmap mindmap = findMindmapById ( mindmapId ) ;
final Label label = mindmap . findLabel ( labelId ) ;
if ( label = = null ) {
2014-03-09 02:32:19 -03:00
mindmapService . linkLabel ( mindmap , delegated ) ;
2014-02-05 02:11:23 -03:00
}
}
}
2012-02-12 21:57:11 -03:00
}