2012-06-16 11:37:40 -03:00
/ *
2015-04-12 00:15:12 -03:00
* Copyright [ 2015 ] [ wisemapping ]
2012-06-16 11:37:40 -03:00
*
* 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 .
* /
package com.wisemapping.service ;
import com.wisemapping.dao.MindmapManager ;
import com.wisemapping.exceptions.WiseMappingException ;
import com.wisemapping.mail.NotificationService ;
import com.wisemapping.model.* ;
import com.wisemapping.security.Utils ;
import org.jetbrains.annotations.NotNull ;
import org.jetbrains.annotations.Nullable ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
2013-02-13 23:13:27 -03:00
import java.io.IOException ;
2013-02-27 23:08:37 -03:00
import java.io.UnsupportedEncodingException ;
2012-11-18 13:25:57 -03:00
import java.util.Calendar ;
import java.util.List ;
2022-01-18 13:16:39 -08:00
import java.util.Optional ;
2012-11-18 13:25:57 -03:00
import java.util.Set ;
2012-06-16 11:37:40 -03:00
public class MindmapServiceImpl
implements MindmapService {
@Autowired
private MindmapManager mindmapManager ;
@Autowired
@Qualifier ( " userService " )
private UserService userService ;
@Autowired
private NotificationService notificationService ;
2012-09-11 21:09:37 -03:00
private String adminUser ;
2012-09-30 17:15:01 -03:00
final private LockManager lockManager ;
public MindmapServiceImpl ( ) {
this . lockManager = new LockManagerImpl ( ) ;
}
2012-09-11 21:09:37 -03:00
2012-06-16 11:37:40 -03:00
@Override
public boolean hasPermissions ( @Nullable User user , int mapId , @NotNull CollaborationRole grantedRole ) {
2012-08-15 21:28:51 -03:00
final Mindmap map = mindmapManager . getMindmapById ( mapId ) ;
2012-06-16 11:37:40 -03:00
return hasPermissions ( user , map , grantedRole ) ;
}
@Override
2012-08-15 21:28:51 -03:00
public boolean hasPermissions ( @Nullable User user , @Nullable Mindmap map , @NotNull CollaborationRole role ) {
2012-06-16 11:37:40 -03:00
boolean result = false ;
if ( map ! = null ) {
2013-03-24 16:58:18 -03:00
if ( ( map . isPublic ( ) & & role = = CollaborationRole . VIEWER ) | | isAdmin ( user ) ) {
2012-06-16 11:37:40 -03:00
result = true ;
} else if ( user ! = null ) {
2022-01-18 13:16:39 -08:00
final Optional < Collaboration > collaboration = map . findCollaboration ( user ) ;
if ( collaboration . isPresent ( ) ) {
result = collaboration
. get ( )
. hasPermissions ( role ) ;
2012-06-16 11:37:40 -03:00
}
}
}
return result ;
}
2013-03-24 16:30:38 -03:00
public boolean isAdmin ( @Nullable User user ) {
2012-09-13 00:35:46 -03:00
return user ! = null & & user . getEmail ( ) ! = null & & user . getEmail ( ) . equals ( adminUser ) ;
2012-09-11 21:09:37 -03:00
}
2013-03-24 22:49:55 -03:00
@Override
2013-03-29 15:44:49 -03:00
public void purgeHistory ( int mapId ) throws IOException {
2013-03-28 12:28:53 -03:00
mindmapManager . purgeHistory ( mapId ) ;
2013-03-24 22:49:55 -03:00
}
2012-06-16 11:37:40 -03:00
@Override
2012-08-15 21:28:51 -03:00
public Mindmap getMindmapByTitle ( String title , User user ) {
2012-06-16 11:37:40 -03:00
return mindmapManager . getMindmapByTitle ( title , user ) ;
}
@Override
2013-03-29 21:09:28 -03:00
@Nullable
2012-09-15 11:35:53 -03:00
public Mindmap findMindmapById ( int id ) {
return mindmapManager . getMindmapById ( id ) ;
2012-06-16 11:37:40 -03:00
}
2022-01-18 13:16:39 -08:00
@NotNull
@Override
public List < Mindmap > findMindmapsByUser ( @NotNull User user ) {
return mindmapManager . findMindmapByUser ( user ) ;
}
2012-06-16 11:37:40 -03:00
@Override
2012-06-17 21:43:34 -03:00
public List < Collaboration > findCollaborations ( @NotNull User user ) {
return mindmapManager . findCollaboration ( user . getId ( ) ) ;
2012-06-16 11:37:40 -03:00
}
@Override
2012-08-15 21:28:51 -03:00
public void updateMindmap ( @NotNull Mindmap mindMap , boolean saveHistory ) throws WiseMappingException {
2012-06-16 11:37:40 -03:00
if ( mindMap . getTitle ( ) = = null | | mindMap . getTitle ( ) . length ( ) = = 0 ) {
2013-02-27 23:08:37 -03:00
throw new WiseMappingException ( " The title can not be empty " ) ;
}
// Check that what we received a valid mindmap...
final String xml ;
try {
xml = mindMap . getXmlStr ( ) . trim ( ) ;
} catch ( UnsupportedEncodingException e ) {
2013-03-24 16:58:18 -03:00
throw new WiseMappingException ( " Could not be decoded. " , e ) ;
2013-02-27 23:08:37 -03:00
}
2013-03-02 21:29:26 -03:00
if ( ! xml . endsWith ( " </map> " ) ) {
2013-03-24 16:58:18 -03:00
throw new WiseMappingException ( " Map seems not to be a valid mindmap: ' " + xml + " ' " ) ;
2012-06-16 11:37:40 -03:00
}
2012-09-30 17:15:01 -03:00
2012-06-16 11:37:40 -03:00
mindmapManager . updateMindmap ( mindMap , saveHistory ) ;
}
@Override
2012-08-15 21:28:51 -03:00
public List < Mindmap > search ( MindMapCriteria criteria ) {
2012-06-16 11:37:40 -03:00
return mindmapManager . search ( criteria ) ;
}
@Override
2012-08-15 21:28:51 -03:00
public void removeCollaboration ( @NotNull Mindmap mindmap , @NotNull Collaboration collaboration ) throws CollaborationException {
2012-06-16 11:37:40 -03:00
// remove collaborator association
2012-08-15 21:28:51 -03:00
final Mindmap mindMap = collaboration . getMindMap ( ) ;
2012-06-16 11:37:40 -03:00
final Set < Collaboration > collaborations = mindMap . getCollaborations ( ) ;
2012-11-14 20:17:55 -03:00
final User creator = mindMap . getCreator ( ) ;
2012-11-14 20:33:42 -03:00
if ( creator . identityEquality ( collaboration . getCollaborator ( ) ) ) {
2012-09-19 09:02:21 -03:00
throw new CollaborationException ( " User is the creator and must have ownership permissions.Creator Email: " + mindMap . getCreator ( ) . getEmail ( ) + " ,Collaborator: " + collaboration . getCollaborator ( ) . getEmail ( ) ) ;
2012-06-16 11:37:40 -03:00
}
// When you delete an object from hibernate you have to delete it from *all* collections it exists in...
collaborations . remove ( collaboration ) ;
2013-02-10 16:14:32 -03:00
mindmapManager . removeCollaboration ( collaboration ) ;
2012-06-16 11:37:40 -03:00
}
@Override
2012-08-15 21:28:51 -03:00
public void removeMindmap ( @NotNull Mindmap mindmap , @NotNull User user ) throws WiseMappingException {
2022-02-12 12:47:54 -08:00
if ( mindmap . getCreator ( ) . identityEquality ( user ) ) {
2012-06-16 11:37:40 -03:00
mindmapManager . removeMindmap ( mindmap ) ;
} else {
2022-01-18 13:16:39 -08:00
final Optional < Collaboration > collaboration = mindmap . findCollaboration ( user ) ;
if ( collaboration . isPresent ( ) ) {
this . removeCollaboration ( mindmap , collaboration . get ( ) ) ;
2012-06-16 11:37:40 -03:00
}
}
}
@Override
2022-01-16 20:01:56 -08:00
public void addMindmap ( @NotNull Mindmap map , @NotNull User user ) {
2012-06-16 11:37:40 -03:00
final String title = map . getTitle ( ) ;
if ( title = = null | | title . length ( ) = = 0 ) {
throw new IllegalArgumentException ( " The tile can not be empty " ) ;
}
//noinspection ConstantConditions
if ( user = = null ) {
throw new IllegalArgumentException ( " User can not be null " ) ;
}
final Calendar creationTime = Calendar . getInstance ( ) ;
2012-07-15 00:57:44 -03:00
map . setLastEditor ( user ) ;
2012-06-16 11:37:40 -03:00
map . setCreationTime ( creationTime ) ;
map . setLastModificationTime ( creationTime ) ;
map . setCreator ( user ) ;
// Add map creator with owner permissions ...
final User dbUser = userService . getUserBy ( user . getId ( ) ) ;
final Collaboration collaboration = new Collaboration ( CollaborationRole . OWNER , dbUser , map ) ;
map . getCollaborations ( ) . add ( collaboration ) ;
mindmapManager . addMindmap ( dbUser , map ) ;
}
@Override
2012-08-15 21:28:51 -03:00
public void addCollaboration ( @NotNull Mindmap mindmap , @NotNull String email , @NotNull CollaborationRole role , @Nullable String message )
2012-06-16 11:37:40 -03:00
throws CollaborationException {
// Validate
final Collaborator owner = mindmap . getCreator ( ) ;
if ( owner . getEmail ( ) . equals ( email ) ) {
throw new CollaborationException ( " The user " + owner . getEmail ( ) + " is the owner " ) ;
}
if ( role = = CollaborationRole . OWNER ) {
throw new CollaborationException ( " Ownership can not be modified " ) ;
}
final Set < Collaboration > collaborations = mindmap . getCollaborations ( ) ;
Collaboration collaboration = getCollaborationBy ( email , collaborations ) ;
if ( collaboration = = null ) {
final Collaborator collaborator = addCollaborator ( email ) ;
collaboration = new Collaboration ( role , collaborator , mindmap ) ;
mindmap . getCollaborations ( ) . add ( collaboration ) ;
mindmapManager . saveMindmap ( mindmap ) ;
// Notify by email ...
final User user = Utils . getUser ( ) ;
notificationService . newCollaboration ( collaboration , mindmap , user , message ) ;
} else if ( collaboration . getRole ( ) ! = role ) {
// If the relationship already exists and the role changed then only update the role
collaboration . setRole ( role ) ;
mindmapManager . updateMindmap ( mindmap , false ) ;
}
}
private Collaborator addCollaborator ( String email ) {
// Add a new collaborator ...
2012-06-17 23:21:02 -03:00
Collaborator collaborator = mindmapManager . findCollaborator ( email ) ;
2012-06-16 11:37:40 -03:00
if ( collaborator = = null ) {
collaborator = new Collaborator ( ) ;
collaborator . setEmail ( email ) ;
collaborator . setCreationDate ( Calendar . getInstance ( ) ) ;
mindmapManager . addCollaborator ( collaborator ) ;
}
return collaborator ;
}
2012-06-17 23:21:02 -03:00
@Override
2012-06-17 19:16:39 -03:00
public List < MindMapHistory > findMindmapHistory ( int mindmapId ) {
2012-06-16 11:37:40 -03:00
return mindmapManager . getHistoryFrom ( mindmapId ) ;
}
2012-06-17 23:21:02 -03:00
@Override
2012-08-15 21:28:51 -03:00
public void revertChange ( @NotNull Mindmap mindmap , int historyId )
2013-02-13 23:13:27 -03:00
throws WiseMappingException , IOException {
2012-06-16 11:37:40 -03:00
final MindMapHistory history = mindmapManager . getHistory ( historyId ) ;
2013-03-29 14:51:21 -03:00
mindmap . setZippedXml ( history . getZippedXml ( ) ) ;
2012-06-17 19:16:39 -03:00
updateMindmap ( mindmap , true ) ;
}
@Override
public MindMapHistory findMindmapHistory ( int id , int hid ) throws WiseMappingException {
final List < MindMapHistory > mindmapHistory = this . findMindmapHistory ( id ) ;
MindMapHistory result = null ;
for ( MindMapHistory history : mindmapHistory ) {
if ( history . getId ( ) = = hid ) {
result = history ;
break ;
}
}
if ( result = = null ) {
throw new WiseMappingException ( " History could not be found for mapid= " + id + " ,hid " + hid ) ;
}
return result ;
2012-06-16 11:37:40 -03:00
}
2012-06-17 23:21:02 -03:00
@Override
public void updateCollaboration ( @NotNull Collaborator collaborator , @NotNull Collaboration collaboration ) throws WiseMappingException {
2012-11-18 13:25:57 -03:00
if ( ! collaborator . identityEquality ( collaboration . getCollaborator ( ) ) ) {
2012-06-17 23:21:02 -03:00
throw new WiseMappingException ( " No enough permissions for this operation. " ) ;
}
mindmapManager . updateCollaboration ( collaboration ) ;
}
2012-09-30 17:15:01 -03:00
@Override
@NotNull
public LockManager getLockManager ( ) {
return this . lockManager ;
}
2012-11-14 20:17:55 -03:00
private Collaboration getCollaborationBy ( @NotNull final String email , @NotNull final Set < Collaboration > collaborations ) {
2012-06-16 11:37:40 -03:00
Collaboration collaboration = null ;
for ( Collaboration user : collaborations ) {
if ( user . getCollaborator ( ) . getEmail ( ) . equals ( email ) ) {
collaboration = user ;
break ;
}
}
return collaboration ;
}
public void setMindmapManager ( MindmapManager mindmapManager ) {
this . mindmapManager = mindmapManager ;
}
public void setUserService ( UserService userService ) {
this . userService = userService ;
}
public void setNotificationService ( NotificationService notificationService ) {
this . notificationService = notificationService ;
}
2012-09-11 21:09:37 -03:00
2012-09-11 21:22:38 -03:00
public void setAdminUser ( @NotNull String adminUser ) {
2012-09-11 21:09:37 -03:00
this . adminUser = adminUser ;
}
2012-09-13 00:35:46 -03:00
@NotNull
public String getAdminUser ( ) {
2012-09-11 21:09:37 -03:00
return adminUser ;
}
2012-06-16 11:37:40 -03:00
}