293 lines
11 KiB
Java
Raw Normal View History

/*
* Copyright [2011] [wisemapping]
*
2011-01-23 21:03:12 -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.
*/
2009-06-07 18:59:43 +00:00
package com.wisemapping.service;
import com.wisemapping.dao.MindmapManager;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.mail.Mailer;
import com.wisemapping.model.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2009-06-07 18:59:43 +00:00
import java.io.IOException;
import java.util.*;
public class MindmapServiceImpl
implements MindmapService {
private MindmapManager mindmapManager;
private UserService userService;
private Mailer mailer;
2012-06-09 15:49:19 -03:00
public boolean isAllowedToCollaborate(@NotNull User user, int mapId, @NotNull CollaborationRole grantedRole) {
2009-06-07 18:59:43 +00:00
final MindMap map = mindmapManager.getMindmapById(mapId);
return isAllowedToCollaborate(user, map, grantedRole);
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
public boolean isAllowedToView(User user, int mapId, CollaborationRole grantedRole) {
2009-06-07 18:59:43 +00:00
final MindMap map = mindmapManager.getMindmapById(mapId);
return isAllowedToView(user, map, grantedRole);
}
2012-06-09 15:49:19 -03:00
public boolean isAllowedToView(@NotNull User user, @NotNull MindMap map, @NotNull CollaborationRole grantedRole) {
boolean result = false;
2009-06-07 18:59:43 +00:00
if (map != null) {
if (map.isPublic()) {
result = true;
2009-06-07 18:59:43 +00:00
} else if (user != null) {
result = isAllowedToCollaborate(user, map, grantedRole);
2009-06-07 18:59:43 +00:00
}
}
return result;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
public boolean isAllowedToCollaborate(@NotNull User user, @Nullable MindMap map, CollaborationRole grantedRole) {
2009-06-07 18:59:43 +00:00
boolean isAllowed = false;
if (map != null) {
if (map.getOwner().getId() == user.getId()) {
isAllowed = true;
} else {
2012-06-09 15:49:19 -03:00
final Set<Collaboration> users = map.getCollaborations();
CollaborationRole rol = null;
for (Collaboration collaboration : users) {
if (collaboration.getCollaborator().getId() == user.getId()) {
rol = collaboration.getRole();
2009-06-07 18:59:43 +00:00
break;
}
}
// only if the user has a role for the current map
isAllowed = rol != null &&
(grantedRole.equals(rol) || rol.ordinal() < grantedRole.ordinal());
}
}
return isAllowed;
}
2012-06-09 15:49:19 -03:00
public Collaboration getMindmapUserBy(int mindmapId, User user) {
2009-06-07 18:59:43 +00:00
return mindmapManager.getMindmapUserBy(mindmapId, user);
}
2012-06-09 22:49:54 -03:00
@Override
2009-06-07 18:59:43 +00:00
public MindMap getMindmapByTitle(String title, User user) {
return mindmapManager.getMindmapByTitle(title, user);
}
2012-06-09 22:49:54 -03:00
@Override
2009-06-07 18:59:43 +00:00
public MindMap getMindmapById(int mindmapId) {
return mindmapManager.getMindmapById(mindmapId);
}
2012-06-09 22:49:54 -03:00
@Override
public List<Collaboration> getCollaborationsBy(@NotNull User user) {
return mindmapManager.getMindmapUserByCollaborator(user.getId());
2009-06-07 18:59:43 +00:00
}
2012-06-09 22:49:54 -03:00
@Override
2009-06-07 18:59:43 +00:00
public void updateMindmap(MindMap mindMap, boolean saveHistory) throws WiseMappingException {
if (mindMap.getTitle() == null || mindMap.getTitle().length() == 0) {
throw new WiseMappingException("The tile can not be empty");
}
2009-06-07 18:59:43 +00:00
mindmapManager.updateMindmap(mindMap, saveHistory);
}
2012-06-09 22:49:54 -03:00
@Override
2009-06-07 18:59:43 +00:00
public List<MindMap> search(MindMapCriteria criteria) {
return mindmapManager.search(criteria);
}
2012-06-09 22:49:54 -03:00
@Override
2012-06-09 22:55:55 -03:00
public void removeCollaboration(@NotNull Collaboration collaboration) throws CollaborationException {
2012-06-09 15:49:19 -03:00
// remove collaborator association
2012-06-09 22:49:54 -03:00
final MindMap mindMap = collaboration.getMindMap();
2012-06-09 22:55:55 -03:00
final Set<Collaboration> collaborations = mindMap.getCollaborations();
2012-06-09 22:49:54 -03:00
// When you delete an object from hibernate you have to delete it from *all* collections it exists in...
2012-06-09 22:55:55 -03:00
if (mindMap.getOwner().getEmail().equals(collaboration.getCollaborator().getEmail())) {
throw new CollaborationException("User is the creator and must have ownership permissions");
}
2012-06-09 22:49:54 -03:00
mindmapManager.removeCollaboration(collaboration);
2012-06-09 22:55:55 -03:00
collaborations.remove(collaboration);
2009-06-07 18:59:43 +00:00
}
2012-06-09 22:49:54 -03:00
@Override
2012-03-15 01:21:46 -03:00
public void removeMindmap(@NotNull MindMap mindmap, @NotNull User user) throws WiseMappingException {
2009-06-07 18:59:43 +00:00
if (mindmap.getOwner().equals(user)) {
mindmapManager.removeMindmap(mindmap);
} else {
2012-06-09 22:55:55 -03:00
final Collaboration collaboration = mindmap.findCollaboration(user);
2012-06-09 22:49:54 -03:00
if (collaboration != null) {
this.removeCollaboration(collaboration);
}
2009-06-07 18:59:43 +00:00
}
}
2012-06-09 22:49:54 -03:00
@Override
public void addMindmap(@NotNull MindMap map, @NotNull User user) throws WiseMappingException {
2009-06-07 18:59:43 +00:00
final String title = map.getTitle();
if (title == null || title.length() == 0) {
throw new IllegalArgumentException("The tile can not be empty");
}
2012-03-15 01:21:46 -03:00
if (user == null) {
2009-06-07 18:59:43 +00:00
throw new IllegalArgumentException("User can not be null");
}
final Calendar creationTime = Calendar.getInstance();
final String username = user.getUsername();
map.setCreator(username);
map.setLastModifierUser(username);
map.setCreationTime(creationTime);
map.setLastModificationTime(creationTime);
map.setOwner(user);
2012-06-09 22:55:55 -03:00
// Add map creator with owner permissions ...
final User dbUser = userService.getUserBy(user.getId());
2012-06-09 15:49:19 -03:00
final Collaboration collaboration = new Collaboration(CollaborationRole.OWNER, dbUser, map);
map.getCollaborations().add(collaboration);
2009-06-07 18:59:43 +00:00
2012-06-06 00:48:46 -03:00
mindmapManager.addMindmap(dbUser, map);
2009-06-07 18:59:43 +00:00
}
2012-06-09 22:49:54 -03:00
@Override
public void addCollaboration(@NotNull MindMap mindmap, @NotNull String email, @NotNull CollaborationRole role)
2012-06-09 22:55:55 -03:00
throws CollaborationException {
2009-06-07 18:59:43 +00:00
2012-06-09 22:49:54 -03:00
// Validate
final Collaborator owner = mindmap.getOwner();
final Set<Collaboration> collaborations = mindmap.getCollaborations();
if (owner.getEmail().equals(email)) {
2012-06-09 22:55:55 -03:00
throw new CollaborationException("The user " + owner.getEmail() + " is the owner");
}
if (role == CollaborationRole.OWNER) {
throw new CollaborationException("Ownership can not be modified");
2012-06-09 22:49:54 -03:00
}
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);
// Sent collaboration email ...
final Map<String, Object> model = new HashMap<String, Object>();
model.put("role", role);
model.put("map", mindmap);
model.put("message", "message");
mailer.sendEmail(mailer.getSiteEmail(), email, "Collaboration", model, "newColaborator.vm");
} 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 ...
Collaborator collaborator = mindmapManager.getCollaboratorBy(email);
if (collaborator == null) {
collaborator = new Collaborator();
collaborator.setEmail(email);
collaborator.setCreationDate(Calendar.getInstance());
mindmapManager.addCollaborator(collaborator);
2009-06-07 18:59:43 +00:00
}
2012-06-09 22:49:54 -03:00
return collaborator;
2009-06-07 18:59:43 +00:00
}
2012-06-09 22:49:54 -03:00
@Override
public void addTags(@NotNull MindMap mindmap, String tags) {
2009-06-07 18:59:43 +00:00
mindmap.setTags(tags);
mindmapManager.updateMindmap(mindmap, false);
if (tags != null && tags.length() > 0) {
final String tag[] = tags.split(TAG_SEPARATOR);
final User user = mindmap.getOwner();
// Add new Tags to User
boolean updateUser = false;
for (String userTag : tag) {
if (!user.getTags().contains(userTag)) {
user.getTags().add(userTag);
updateUser = true;
}
}
if (updateUser) {
//update user
userService.updateUser(user);
}
}
}
public void addWelcomeMindmap(User user) throws WiseMappingException {
final MindMap savedWelcome = getMindmapById(Constants.WELCOME_MAP_ID);
// Is there a welcomed map configured ?
2012-03-15 01:21:46 -03:00
if (savedWelcome != null) {
2009-06-07 18:59:43 +00:00
final MindMap welcomeMap = new MindMap();
welcomeMap.setTitle(savedWelcome.getTitle() + " " + user.getFirstname());
welcomeMap.setDescription(savedWelcome.getDescription());
welcomeMap.setXml(savedWelcome.getXml());
addMindmap(welcomeMap, user);
}
}
public List<MindMapHistory> getMindMapHistory(int mindmapId) {
return mindmapManager.getHistoryFrom(mindmapId);
}
public void revertMapToHistory(MindMap map, int historyId)
throws IOException, WiseMappingException {
final MindMapHistory history = mindmapManager.getHistory(historyId);
map.setXml(history.getXml());
2009-06-07 18:59:43 +00:00
updateMindmap(map, false);
}
2012-06-09 22:49:54 -03:00
private Collaboration getCollaborationBy(String email, Set<Collaboration> collaborations) {
2012-06-09 15:49:19 -03:00
Collaboration collaboration = null;
2009-06-07 18:59:43 +00:00
2012-06-09 15:49:19 -03:00
for (Collaboration user : collaborations) {
if (user.getCollaborator().getEmail().equals(email)) {
2012-06-09 15:49:19 -03:00
collaboration = user;
2009-06-07 18:59:43 +00:00
break;
}
}
2012-06-09 15:49:19 -03:00
return collaboration;
2009-06-07 18:59:43 +00:00
}
public void setMindmapManager(MindmapManager mindmapManager) {
this.mindmapManager = mindmapManager;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void setMailer(Mailer mailer) {
this.mailer = mailer;
}
}