Improve collaboration mail.

This commit is contained in:
Paulo Gustavo Veiga
2012-06-13 23:04:29 -03:00
parent c4d2acec7b
commit 226a7a0ff9
23 changed files with 260 additions and 137 deletions

View File

@@ -36,8 +36,7 @@ public class UserManagerImpl
private PasswordEncoder passwordEncoder;
public void setEncoder(PasswordEncoder passwordEncoder)
{
public void setEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
@@ -47,14 +46,12 @@ public class UserManagerImpl
@Override
public User getUserBy(final String email) {
final User user;
final List users = getHibernateTemplate().find("from com.wisemapping.model.User colaborator where email=?", email);
public User getUserBy(@NotNull final String email) {
User user = null;
final List<User> users = getHibernateTemplate().find("from com.wisemapping.model.User colaborator where email=?", email);
if (users != null && !users.isEmpty()) {
assert users.size() == 1 : "More than one user with the same email!";
user = (User) users.get(0);
} else {
user = null;
user = users.get(0);
}
return user;
}
@@ -100,13 +97,13 @@ public class UserManagerImpl
@Override
public void createUser(User user) {
assert user != null : "Trying to store a null user";
user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
getHibernateTemplate().saveOrUpdate(user);
}
@Override
public User createUser(@NotNull User user, @NotNull Collaborator col) {
user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
assert user != null : "Trying to store a null user";
final Set<Collaboration> set = col.getCollaborations();
@@ -139,7 +136,7 @@ public class UserManagerImpl
public void updateUser(User user) {
assert user != null : "user is null";
user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
getHibernateTemplate().update(user);
}

View File

@@ -39,19 +39,16 @@ public final class Mailer {
//~ Methods ..............................................................................................
public Mailer(String registrationEmail, String siteEmail)
{
public Mailer(String registrationEmail, String siteEmail) {
this.registrationEmail = registrationEmail;
this.siteEmail = siteEmail;
}
public String getRegistrationEmail()
{
public String getRegistrationEmail() {
return registrationEmail;
}
public String getSiteEmail()
{
public String getSiteEmail() {
return siteEmail;
}
@@ -66,11 +63,12 @@ public final class Mailer {
message.setFrom(from);
message.setSubject(subject);
final String text =
final String messageBody =
VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/mail/" + templateMail,
model);
System.out.println(message);
message.setText(text, true);
message.setText(messageBody, true);
}
};

View File

@@ -0,0 +1,83 @@
/*
* Copyright [2011] [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.
*/
package com.wisemapping.mail;
import com.wisemapping.model.Collaboration;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.Map;
final public class NotificationService {
@Autowired
private Mailer mailer;
private String baseUrl;
public NotificationService() {
}
public void newCollaboration(@NotNull Collaboration collaboration, @NotNull MindMap mindmap, @NotNull User user, @Nullable String message) {
try {
// Sent collaboration email ...
final String formMail = mailer.getSiteEmail();
// Is the user already registered user ?.
final String collabEmail = collaboration.getCollaborator().getEmail();
// Build the subject ...
final String subject = user.getFullName() + " has shared a mindmap with you";
// Fill template properties ...
final Map<String, Object> model = new HashMap<String, Object>();
model.put("mindmap", mindmap);
model.put("message", "message");
model.put("ownerName", user.getFirstname());
model.put("mapEditUrl", baseUrl + "/c/maps/" + mindmap.getId() + "/edit");
model.put("baseUrl", baseUrl + "/c/maps/" + mindmap.getId() + "/edit");
mailer.sendEmail(formMail, collabEmail, subject, model, "newCollaboration.vm");
} catch (Exception e) {
handleException(e);
}
}
private void handleException(Exception e) {
e.printStackTrace();
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
public void setMailer(Mailer mailer) {
this.mailer = mailer;
}
}

View File

@@ -45,6 +45,10 @@ public class User
return tags;
}
public String getFullName() {
return this.getFirstname() + " " + this.getLastname();
}
public String getFirstname() {
return firstname;
}

View File

@@ -238,7 +238,7 @@ public class MindmapController extends BaseController {
// Remove all collaborations that no applies anymore ..
for (final Collaboration collaboration : collabsToRemove) {
mindmapService.removeCollaboration(collaboration);
mindmapService.removeCollaboration(mindMap, collaboration);
}
}

View File

@@ -25,6 +25,8 @@ import com.wisemapping.exceptions.UnexpectedArgumentException;
import com.wisemapping.security.Utils;
import com.wisemapping.service.MindmapService;
import org.aopalliance.intercept.MethodInvocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class BaseSecurityAdvice {
private MindmapService mindmapService = null;
@@ -47,9 +49,9 @@ public abstract class BaseSecurityAdvice {
}
}
protected abstract boolean isAllowed(User user, MindMap map);
protected abstract boolean isAllowed(@Nullable User user, MindMap map);
protected abstract boolean isAllowed(User user, int mapId);
protected abstract boolean isAllowed(@Nullable User user, int mapId);
protected MindmapService getMindmapService() {
return mindmapService;

View File

@@ -25,6 +25,7 @@ import com.wisemapping.model.MindMap;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class UpdateSecurityAdvise
extends BaseSecurityAdvice
@@ -35,7 +36,7 @@ public class UpdateSecurityAdvise
return methodInvocation.proceed();
}
protected boolean isAllowed(@NotNull User user, @NotNull MindMap map) {
protected boolean isAllowed(@Nullable User user, @NotNull MindMap map) {
boolean result;
if (map.getCreator() == null) {
// This means that the map is new and is an add operation.
@@ -46,7 +47,7 @@ public class UpdateSecurityAdvise
return result;
}
protected boolean isAllowed(User user, int mapId) {
protected boolean isAllowed(@Nullable User user, int mapId) {
return getMindmapService().hasPermissions(user, mapId, CollaborationRole.EDITOR);
}
}

View File

@@ -34,11 +34,11 @@ public class ViewBaseSecurityAdvise
return methodInvocation.proceed();
}
protected boolean isAllowed(User user, MindMap map) {
protected boolean isAllowed(@NotNull User user, MindMap map) {
return getMindmapService().hasPermissions(user, map, CollaborationRole.VIEWER);
}
protected boolean isAllowed(User user, int mapId) {
protected boolean isAllowed(@NotNull User user, int mapId) {
return getMindmapService().hasPermissions(user, mapId, CollaborationRole.VIEWER);
}
}

View File

@@ -21,6 +21,7 @@ package com.wisemapping.service;
import com.wisemapping.model.*;
import com.wisemapping.exceptions.WiseMappingException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.io.IOException;
@@ -42,7 +43,7 @@ public interface MindmapService {
public void addCollaboration(@NotNull MindMap mindmap, @NotNull String email, @NotNull CollaborationRole role)
throws CollaborationException;
public void removeCollaboration(@NotNull Collaboration collaboration) throws CollaborationException;
public void removeCollaboration(@NotNull MindMap mindmap, @NotNull Collaboration collaboration) throws CollaborationException;
public void addTags(MindMap mindmap, String tags);
@@ -52,9 +53,9 @@ public interface MindmapService {
public List<MindMapHistory> getMindMapHistory(int mindmapId);
public boolean hasPermissions(User user, MindMap map, CollaborationRole allowedRole);
public boolean hasPermissions(@Nullable User user, MindMap map, CollaborationRole allowedRole);
public boolean hasPermissions(User user, int mapId, CollaborationRole allowedRole);
public boolean hasPermissions(@Nullable User user, int mapId, CollaborationRole allowedRole);
public void addWelcomeMindmap(User user) throws WiseMappingException;

View File

@@ -21,9 +21,13 @@ package com.wisemapping.service;
import com.wisemapping.dao.MindmapManager;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.mail.Mailer;
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;
import java.io.IOException;
import java.util.*;
@@ -32,9 +36,15 @@ import java.util.*;
public class MindmapServiceImpl
implements MindmapService {
@Autowired
private MindmapManager mindmapManager;
@Autowired
@Qualifier("userService")
private UserService userService;
private Mailer mailer;
@Autowired
private NotificationService notificationService;
@Override
public boolean hasPermissions(@NotNull User user, int mapId, @NotNull CollaborationRole grantedRole) {
@@ -89,7 +99,7 @@ public class MindmapServiceImpl
}
@Override
public void removeCollaboration(@NotNull Collaboration collaboration) throws CollaborationException {
public void removeCollaboration(@NotNull MindMap mindmap, @NotNull Collaboration collaboration) throws CollaborationException {
// remove collaborator association
final MindMap mindMap = collaboration.getMindMap();
final Set<Collaboration> collaborations = mindMap.getCollaborations();
@@ -110,7 +120,7 @@ public class MindmapServiceImpl
} else {
final Collaboration collaboration = mindmap.findCollaboration(user);
if (collaboration != null) {
this.removeCollaboration(collaboration);
this.removeCollaboration(mindmap, collaboration);
}
}
}
@@ -166,16 +176,9 @@ public class MindmapServiceImpl
mindmap.getCollaborations().add(collaboration);
mindmapManager.saveMindmap(mindmap);
try {
// 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");
} catch (Exception e) {
e.printStackTrace();
}
// Notify by email ...
final User user = Utils.getUser();
notificationService.newCollaboration(collaboration, mindmap, user, null);
} else if (collaboration.getRole() != role) {
// If the relationship already exists and the role changed then only update the role
@@ -264,7 +267,7 @@ public class MindmapServiceImpl
this.userService = userService;
}
public void setMailer(Mailer mailer) {
this.mailer = mailer;
public void setNotificationService(NotificationService notificationService) {
this.notificationService = notificationService;
}
}