Add wisemapping support for import.

This commit is contained in:
Paulo Gustavo Veiga
2012-06-06 00:48:46 -03:00
parent 88b0efa859
commit 79b009e29e
23 changed files with 214 additions and 339 deletions

View File

@@ -1,63 +0,0 @@
/*
* 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.controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wisemapping.model.MindMap;
/**
* Usage: http://localhost:8080/wisemapping/c/cooker?action=edit&mapId=12
*/
public class MindmapCooker extends BaseMultiActionController {
public static final String MINDMAP_ID_PARAMETER = "mapId";
public static final String MAP_XML_PARAM = "mapXml";
public ModelAndView edit(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
final String mindmapId = httpServletRequest.getParameter(MINDMAP_ID_PARAMETER);
final int mapId = Integer.parseInt(mindmapId);
final MindMap mindmap = getMindmapService().getMindmapById(mapId);
// Mark as try mode...
final ModelAndView view = new ModelAndView("mindmapCooker", "mindmap", mindmap);
return view;
}
public ModelAndView save(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
final String mindmapId = httpServletRequest.getParameter(MINDMAP_ID_PARAMETER);
final int mapId = Integer.parseInt(mindmapId);
final MindMap mindmap = getMindmapService().getMindmapById(mapId);
String xml = httpServletRequest.getParameter("xml");
mindmap.setXmlStr(xml);
getMindmapService().updateMindmap(mindmap, false);
// Mark as try mode...
final ModelAndView view = new ModelAndView("mindmapCooker", "mindmap", mindmap);
return view;
}
}

View File

@@ -1,70 +0,0 @@
/*
* 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.controller;
import com.wisemapping.filter.UserAgent;
import com.wisemapping.model.MindMap;
import com.wisemapping.security.Utils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MindmapEditorController extends BaseMultiActionController {
public static final String MINDMAP_ID_PARAMETER = "mapId";
public static final String MAP_XML_PARAM = "mapXml";
public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException noSuchRequestHandlingMethodException, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
return new ModelAndView(new RedirectView("maps/"));
}
public ModelAndView open(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView view;
final String mindmapId = httpServletRequest.getParameter(MINDMAP_ID_PARAMETER);
final int mapId = Integer.parseInt(mindmapId);
UserAgent userAgent = UserAgent.create(httpServletRequest);
if(userAgent.needsGCF()){
view = new ModelAndView("gcfPluginNeeded");
view.addObject(MINDMAP_ID_PARAMETER, mindmapId);
}
else{
final MindMap mindmap = getMindmapService().getMindmapById(mapId);
// Mark as try mode...
view = new ModelAndView("mindmapEditor", "mindmap", mindmap);
view.addObject("editorTryMode", false);
final boolean showHelp = isWelcomeMap(mindmap);
view.addObject("showHelp", showHelp);
view.addObject("user", Utils.getUser());
}
return view;
}
private boolean isWelcomeMap(MindMap map) {
return map.getTitle().startsWith("Welcome ");
}
}

View File

@@ -1,42 +0,0 @@
/*
* 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.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wisemapping.model.MindMap;
public class PublicViewController extends BaseMultiActionController {
protected ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException noSuchRequestHandlingMethodException, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws Exception
{
final MindMap mindmap = super.getMindmapFromRequest(httpServletRequest);
final ModelAndView view = new ModelAndView("mindmapPublicView");
view.addObject("mindmap", mindmap);
view.addObject("viewTitle","'"+mindmap.getTitle()+"' Map View");
return view;
}
}

View File

@@ -19,6 +19,7 @@
package com.wisemapping.dao;
import com.wisemapping.model.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
@@ -35,10 +36,10 @@ public class MindmapManagerImpl
public Collaborator getCollaboratorBy(final String email) {
final Collaborator collaborator;
final List colaborators = getHibernateTemplate().find("from com.wisemapping.model.Collaborator collaborator where email=?", email);
if (colaborators != null && !colaborators.isEmpty()) {
assert colaborators.size() == 1 : "More than one user with the same username!";
collaborator = (Collaborator) colaborators.get(0);
final List<Collaborator> collaborators = getHibernateTemplate().find("from com.wisemapping.model.Collaborator collaborator where email=?", email);
if (collaborators != null && !collaborators.isEmpty()) {
assert collaborators.size() == 1 : "More than one user with the same username!";
collaborator = collaborators.get(0);
} else {
collaborator = null;
}
@@ -46,24 +47,22 @@ public class MindmapManagerImpl
}
public List<MindMap> search(MindMapCriteria criteria) {
return search(criteria,-1);
return search(criteria, -1);
}
public List<MindMapHistory> getHistoryFrom(int mindmapId)
{
public List<MindMapHistory> getHistoryFrom(int mindmapId) {
final Criteria hibernateCriteria = getSession().createCriteria(MindMapHistory.class);
hibernateCriteria.add(Restrictions.eq("mindmapId",mindmapId));
hibernateCriteria.addOrder( Order.desc("creationTime"));
hibernateCriteria.add(Restrictions.eq("mindmapId", mindmapId));
hibernateCriteria.addOrder(Order.desc("creationTime"));
// Mientras no haya paginacion solo los 10 primeros
// This line throws errors in some environments, so getting all history and taking firsts 10 records
// hibernateCriteria.setMaxResults(10);
List list = hibernateCriteria.list();
return list.subList(0,(10<list.size()?10:list.size()));
return list.subList(0, (10 < list.size() ? 10 : list.size()));
}
public MindMapHistory getHistory(int historyId)
{
return (MindMapHistory) getHibernateTemplate().get(MindMapHistory.class, historyId);
public MindMapHistory getHistory(int historyId) {
return (MindMapHistory) getHibernateTemplate().get(MindMapHistory.class, historyId);
}
public List<MindMap> search(MindMapCriteria criteria, int maxResult) {
@@ -161,9 +160,8 @@ public class MindmapManagerImpl
return result;
}
public void addView(int mapId)
{
public void addView(int mapId) {
}
public void addMindmap(User user, MindMap mindMap) {
@@ -175,11 +173,10 @@ public class MindmapManagerImpl
getSession().save(mindMap);
}
public void updateMindmap(MindMap mindMap, boolean saveHistory) {
public void updateMindmap(@NotNull MindMap mindMap, boolean saveHistory) {
assert mindMap != null : "Save Mindmap: Mindmap is required!";
getHibernateTemplate().saveOrUpdate(mindMap);
if (saveHistory)
{
if (saveHistory) {
saveHistory(mindMap);
}
}
@@ -188,8 +185,7 @@ public class MindmapManagerImpl
getHibernateTemplate().delete(mindMap);
}
public void saveHistory(MindMap mindMap)
{
public void saveHistory(MindMap mindMap) {
final MindMapHistory history = new MindMapHistory();
history.setXml(mindMap.getXml());

View File

@@ -211,7 +211,10 @@ public class MindMap {
this.creationTime = creationTime;
}
public void setOwner(User owner) {
public void setOwner(@NotNull User owner) {
if (owner == null) {
throw new IllegalArgumentException("Owner can not be null");
}
this.owner = owner;
}
@@ -241,6 +244,10 @@ public class MindMap {
}
public void setStarred(@NotNull Collaborator collaborator, boolean value) {
if(collaborator==null){
throw new IllegalStateException("Collaborator can not be null");
}
CollaboratorProperties collaboratorProperties = this.findUserProperty(collaborator);
if (collaboratorProperties == null) {
collaboratorProperties = new CollaboratorProperties(collaborator, this);

View File

@@ -174,12 +174,6 @@ public class MindmapController extends BaseController {
saveMindmap(minor, mindMap, user);
}
private ValidationException buildValidationException(@NotNull String fieldName, @NotNull String message) throws ValidationException {
final BindingResult result = new BeanPropertyBindingResult(new RestMindmap(), "");
result.rejectValue(fieldName, "error.not-specified", null, message);
return new ValidationException(result);
}
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/title", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
@@ -261,16 +255,17 @@ public class MindmapController extends BaseController {
}
}
private void saveMindmap(boolean minor, @NotNull final MindMap mindMap, @NotNull final User user) throws WiseMappingException {
final Calendar now = Calendar.getInstance();
mindMap.setLastModificationTime(now);
mindMap.setLastModifierUser(user.getUsername());
mindmapService.updateMindmap(mindMap, minor);
}
@RequestMapping(method = RequestMethod.POST, value = "/maps", consumes = {"application/xml", "application/json"})
@RequestMapping(method = RequestMethod.POST, value = "/maps", consumes = {"application/xml", "application/json", "application/wisemapping+xml"})
@ResponseStatus(value = HttpStatus.CREATED)
public void createMap(@RequestBody RestMindmap restMindmap, @NotNull HttpServletResponse response) throws IOException, WiseMappingException {
public void createMap(@RequestBody RestMindmap restMindmap, @NotNull HttpServletResponse response, @RequestParam(required = false) String title, @RequestParam(required = false) String description) throws IOException, WiseMappingException {
// 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);
}
// Validate ...
final BindingResult result = new BeanPropertyBindingResult(restMindmap, "");
@@ -279,19 +274,16 @@ public class MindmapController extends BaseController {
throw new ValidationException(result);
}
// Some basic validations ...
final User user = Utils.getUser();
// If the user has not specified the xml content, add one ...
final MindMap delegated = restMindmap.getDelegated();
String xml = restMindmap.getXml();
if (xml == null || xml.isEmpty()) {
xml = MindMap.getDefaultMindmapXml(restMindmap.getTitle());
}
delegated.setOwner(user);
delegated.setXmlStr(xml);
// Add new mindmap ...
final User user = Utils.getUser();
mindmapService.addMindmap(delegated, user);
// Return the new created map ...
@@ -306,11 +298,11 @@ public class MindmapController extends BaseController {
// Convert map ...
final Importer importer = ImporterFactory.getInstance().getImporter(ImportFormat.FREEMIND);
final ByteArrayInputStream stream = new ByteArrayInputStream(freemindXml);
final MindMap mindMap = importer.importMap(title, description, stream);
final MindMap mindMap = importer.importMap(title, "", stream);
// Save new map ...
final User user = Utils.getUser();
createMap(new RestMindmap(mindMap, user), response);
createMap(new RestMindmap(mindMap, user), response, title, description);
}
@RequestMapping(method = RequestMethod.POST, value = "/maps/{id}", consumes = {"application/xml", "application/json"})
@@ -340,4 +332,17 @@ public class MindmapController extends BaseController {
response.setHeader("Location", "/service/maps/" + clonedMap.getId());
response.setHeader("ResourceId", Integer.toString(clonedMap.getId()));
}
private void saveMindmap(boolean minor, @NotNull final MindMap mindMap, @NotNull final User user) throws WiseMappingException {
final Calendar now = Calendar.getInstance();
mindMap.setLastModificationTime(now);
mindMap.setLastModifierUser(user.getUsername());
mindmapService.updateMindmap(mindMap, minor);
}
private ValidationException buildValidationException(@NotNull String fieldName, @NotNull String message) throws ValidationException {
final BindingResult result = new BeanPropertyBindingResult(new RestMindmap(), "");
result.rejectValue(fieldName, "error.not-specified", null, message);
return new ValidationException(result);
}
}

View File

@@ -4,6 +4,7 @@ package com.wisemapping.rest.model;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.User;
import com.wisemapping.security.Utils;
import org.codehaus.jackson.annotate.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -38,7 +39,7 @@ public class RestMindmap {
}
public RestMindmap() {
this(new MindMap(), null);
this(new MindMap(), Utils.getUser());
}
@@ -56,7 +57,6 @@ public class RestMindmap {
return result;
}
public String getDescription() {
return mindmap.getDescription();
}
@@ -161,7 +161,9 @@ public class RestMindmap {
}
public void setStarred(boolean value) {
mindmap.setStarred(collaborator, value);
if (collaborator != null) {
mindmap.setStarred(collaborator, value);
}
}
@JsonIgnore

View File

@@ -41,6 +41,7 @@ final public class Utils {
return result;
}
@NotNull
public static User getUser() {
User result = null;
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
@@ -51,6 +52,10 @@ final public class Utils {
result = ((UserDetails)principal).getUser();
}
}
if(result==null){
throw new IllegalStateException("User could not be retrieved");
}
return result;
}
}

View File

@@ -31,9 +31,7 @@ public abstract class BaseSecurityAdvice {
public void checkRole(MethodInvocation methodInvocation) throws UnexpectedArgumentException, AccessDeniedSecurityException {
final User user = Utils.getUser();
final Object argument = methodInvocation.getArguments()[0];
boolean isAllowed;
if (argument instanceof MindMap) {

View File

@@ -24,11 +24,11 @@ import com.wisemapping.model.MindMap;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jetbrains.annotations.NotNull;
public class UpdateSecurityAdvise
extends BaseSecurityAdvice
implements MethodInterceptor
{
extends BaseSecurityAdvice
implements MethodInterceptor {
private UserRole grantedRole = UserRole.COLLABORATOR;
@@ -37,13 +37,18 @@ public class UpdateSecurityAdvise
return methodInvocation.proceed();
}
protected boolean isAllowed(User user, MindMap map)
{
return getMindmapService().isAllowedToView(user,map,grantedRole);
protected boolean isAllowed(@NotNull User user, @NotNull MindMap map) {
boolean result;
if (map.getOwner() == null) {
// This means that the map is new and is an add operation.
result = true;
} else {
result = getMindmapService().isAllowedToView(user, map, grantedRole);
}
return result;
}
protected boolean isAllowed(User user, int mapId)
{
return getMindmapService().isAllowedToView(user,mapId,grantedRole);
protected boolean isAllowed(User user, int mapId) {
return getMindmapService().isAllowedToView(user, mapId, grantedRole);
}
}

View File

@@ -41,7 +41,7 @@ public interface MindmapService {
public void addMindmap(MindMap map, User user) throws WiseMappingException;
public void addCollaborators(MindMap mindmap, String[] colaboratorEmails, UserRole role, ColaborationEmail email)
public void addCollaborators(MindMap mindmap, String[] collaboratorEmails, UserRole role, ColaborationEmail email)
throws InvalidColaboratorException;
public void addTags(MindMap mindmap, String tags);
@@ -60,7 +60,7 @@ public interface MindmapService {
public boolean isAllowedToView(User user, int mapId, UserRole allowedRole);
public boolean isAllowedToColaborate(User user, int mapId, UserRole grantedRole);
public boolean isAllowedToCollaborate(User user, int mapId, UserRole grantedRole);
public boolean isAllowedToCollaborate(User user, MindMap map, UserRole grantedRole);

View File

@@ -36,7 +36,7 @@ public class MindmapServiceImpl
private UserService userService;
private Mailer mailer;
public boolean isAllowedToColaborate(User user, int mapId, UserRole grantedRole) {
public boolean isAllowedToCollaborate(@NotNull User user, int mapId, @NotNull UserRole grantedRole) {
final MindMap map = mindmapManager.getMindmapById(mapId);
return isAllowedToCollaborate(user, map, grantedRole);
}
@@ -46,7 +46,7 @@ public class MindmapServiceImpl
return isAllowedToView(user, map, grantedRole);
}
public boolean isAllowedToView(User user, MindMap map, UserRole grantedRole) {
public boolean isAllowedToView(@NotNull User user, @NotNull MindMap map, @NotNull UserRole grantedRole) {
boolean result = false;
if (map != null) {
if (map.isPublic()) {
@@ -162,16 +162,16 @@ public class MindmapServiceImpl
final MindmapUser mindmapUser = new MindmapUser(UserRole.OWNER.ordinal(), dbUser, map);
map.getMindmapUsers().add(mindmapUser);
mindmapManager.addMindmap(user, map);
mindmapManager.addMindmap(dbUser, map);
}
public void addCollaborators(MindMap mindmap, String[] colaboratorEmails, UserRole role, ColaborationEmail email)
public void addCollaborators(MindMap mindmap, String[] collaboratorEmails, UserRole role, ColaborationEmail email)
throws InvalidColaboratorException {
if (colaboratorEmails != null && colaboratorEmails.length > 0) {
if (collaboratorEmails != null && collaboratorEmails.length > 0) {
final Collaborator owner = mindmap.getOwner();
final Set<MindmapUser> mindmapUsers = mindmap.getMindmapUsers();
for (String colaboratorEmail : colaboratorEmails) {
for (String colaboratorEmail : collaboratorEmails) {
if (owner.getEmail().equals(colaboratorEmail)) {
throw new InvalidColaboratorException("The user " + owner.getEmail() + " is the owner");
}

View File

@@ -22,6 +22,7 @@ import com.wisemapping.model.MindMap;
import com.wisemapping.model.MindmapUser;
import com.wisemapping.model.UserRole;
import com.wisemapping.model.User;
import com.wisemapping.security.Utils;
import java.text.DateFormat;
import java.util.*;
@@ -57,6 +58,10 @@ public class MindMapBean {
return mindMap.getId();
}
public boolean isStarred() {
return mindMap.isStarred(Utils.getUser());
}
public List<ColaboratorBean> getViewers() {
return viewers;
}