wisemapping-open-source/wise-webapp/src/main/java/com/wisemapping/model/Mindmap.java

330 lines
9.6 KiB
Java
Raw Normal View History

2012-08-17 02:51:59 +02:00
/*
2012-10-05 01:48:01 +02:00
* Copyright [2012] [wisemapping]
2012-08-17 02:51:59 +02: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.model;
import com.wisemapping.exceptions.AccessDeniedSecurityException;
2012-08-17 02:51:59 +02:00
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.util.ZipUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
public class Mindmap {
private static final String UTF_8 = "UTF-8";
//~ Instance fields ......................................................................................
private int id;
private Calendar creationTime;
private String description;
private boolean isPublic;
private Calendar lastModificationTime;
private User lastEditor;
private Set<Collaboration> collaborations = new HashSet<Collaboration>();
2014-01-28 06:28:16 +01:00
private Set<Label> labels = new HashSet<Label>();
2012-08-17 02:51:59 +02:00
private User creator;
private String tags;
private String title;
2013-03-30 05:09:28 +01:00
private byte[] zippedXml;
2012-08-17 02:51:59 +02:00
//~ Constructors .........................................................................................
public Mindmap() {
}
//~ Methods ..............................................................................................
2013-03-30 05:09:28 +01:00
public void setUnzipXml(@NotNull byte[] value) {
try {
final byte[] zip = ZipUtils.bytesToZip(value);
this.setZippedXml(zip);
} catch (IOException e) {
throw new IllegalStateException(e);
}
2012-08-17 02:51:59 +02:00
}
2013-03-30 05:09:28 +01:00
public void setXmlStr(@NotNull String xml) {
try {
this.setUnzipXml(xml.getBytes(UTF_8));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
2012-08-17 02:51:59 +02:00
}
}
2013-03-30 05:09:28 +01:00
@NotNull
public byte[] getUnzipXml() {
byte[] result = new byte[]{};
if (zippedXml != null) {
2013-03-29 18:57:21 +01:00
try {
2013-03-30 05:09:28 +01:00
final byte[] zip = this.getZippedXml();
result = ZipUtils.zipToBytes(zip);
2013-03-29 18:57:21 +01:00
} catch (IOException e) {
throw new IllegalStateException(e);
}
2012-08-17 02:51:59 +02:00
}
return result;
}
2013-03-30 05:09:28 +01:00
@NotNull
public String getXmlStr() throws UnsupportedEncodingException {
return new String(this.getUnzipXml(), UTF_8);
}
@NotNull
public byte[] getZippedXml() {
return zippedXml;
}
public void setZippedXml(@NotNull byte[] value) {
this.zippedXml = value;
2012-08-17 02:51:59 +02:00
}
public Set<Collaboration> getCollaborations() {
return collaborations;
}
public void setCollaborations(Set<Collaboration> collaborations) {
this.collaborations = collaborations;
}
public void addCollaboration(@NotNull Collaboration collaboration) {
collaborations.add(collaboration);
}
public void removedCollaboration(@NotNull Collaboration collaboration) {
collaborations.add(collaboration);
}
2014-01-28 06:28:16 +01:00
@NotNull public Set<Label> getLabels() {
return labels;
}
public void setLabels(@NotNull final Set<Label> labels) {
this.labels = labels;
}
public void addLabel(@NotNull final Label label) {
this.labels.add(label);
}
2012-08-17 02:51:59 +02:00
@Nullable
public Collaboration findCollaboration(@NotNull Collaborator collaborator) {
2012-11-15 00:17:55 +01:00
Collaboration result = null;
for (Collaboration collaboration : collaborations) {
2012-11-15 00:33:42 +01:00
if (collaboration.getCollaborator().identityEquality(collaborator)) {
2012-11-15 00:17:55 +01:00
result = collaboration;
break;
}
}
return result;
2012-08-17 02:51:59 +02:00
}
@Nullable
public Collaboration findCollaboration(@NotNull String email) {
Collaboration result = null;
for (Collaboration collaboration : collaborations) {
if (collaboration.getCollaborator().getEmail().equals(email)) {
result = collaboration;
break;
}
}
return result;
}
public boolean isPublic() {
return isPublic;
}
public void setPublic(boolean isPublic) {
this.isPublic = isPublic;
}
2013-03-30 05:09:28 +01:00
@NotNull
2012-08-17 02:51:59 +02:00
public Calendar getLastModificationTime() {
return lastModificationTime;
}
public void setLastModificationTime(Calendar lastModificationTime) {
this.lastModificationTime = lastModificationTime;
}
@Nullable
public User getLastEditor() {
return lastEditor;
}
public void setLastEditor(@Nullable User lastEditor) {
this.lastEditor = lastEditor;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
2013-02-01 00:01:15 +01:00
@NotNull
2012-08-17 02:51:59 +02:00
public String getXmlAsJsLiteral()
throws IOException {
String xml = this.getXmlStr();
if (xml != null) {
2012-11-29 01:44:17 +01:00
xml = xml.replace("'", "\\'");
xml = xml.replace("\n", "\\n");
xml = xml.replace("\r", "");
2013-02-01 00:01:15 +01:00
xml = xml.replace("\\b", "\\\\b");
xml = xml.replace("\\t", "\\\\t");
xml = xml.replace("\\r", "\\\\r");
2013-02-01 00:27:17 +01:00
xml = xml.replace("\\f", "\\\\f");
2013-02-01 00:01:15 +01:00
2012-11-29 01:44:17 +01:00
xml = xml.trim();
2012-08-17 02:51:59 +02:00
}
return xml;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getTags() {
return tags;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Calendar getCreationTime() {
return creationTime;
}
public void setCreationTime(Calendar creationTime) {
this.creationTime = creationTime;
}
public void setCreator(@NotNull User creator) {
this.creator = creator;
}
public User getCreator() {
return creator;
}
private CollaborationProperties findUserProperty(@NotNull Collaborator collaborator) {
final Collaboration collaboration = this.findCollaboration(collaborator);
return collaboration != null ? collaboration.getCollaborationProperties() : null;
}
public void setStarred(@NotNull Collaborator collaborator, boolean value) throws WiseMappingException {
final CollaborationProperties collaborationProperties = findCollaborationProperties(collaborator);
collaborationProperties.setStarred(value);
}
@NotNull
public CollaborationProperties findCollaborationProperties(@NotNull Collaborator collaborator) throws WiseMappingException {
2012-09-15 16:35:53 +02:00
return this.findCollaborationProperties(collaborator, true);
}
@Nullable
public CollaborationProperties findCollaborationProperties(@NotNull Collaborator collaborator, boolean forceCheck) throws WiseMappingException {
2012-08-17 02:51:59 +02:00
if (collaborator == null) {
throw new IllegalStateException("Collaborator can not be null");
}
final Collaboration collaboration = this.findCollaboration(collaborator);
2012-09-15 16:35:53 +02:00
CollaborationProperties result = null;
if (collaboration != null) {
result = collaboration.getCollaborationProperties();
} else {
if (forceCheck)
throw new AccessDeniedSecurityException("Collaborator " + collaborator.getEmail() + " could not access " + this.getId());
2012-08-17 02:51:59 +02:00
}
2012-09-15 16:35:53 +02:00
return result;
2012-08-17 02:51:59 +02:00
}
public boolean isStarred(@NotNull Collaborator collaborator) {
final CollaborationProperties collaboratorProperty = this.findUserProperty(collaborator);
return collaboratorProperty != null && collaboratorProperty.getStarred();
}
public static String getDefaultMindmapXml(@NotNull final String title) {
final StringBuilder result = new StringBuilder();
result.append("<map version=\"tango\">");
result.append("<topic central=\"true\" text=\"");
result.append(StringEscapeUtils.escapeXml(title));
result.append("\"/></map>");
return result.toString();
}
public Mindmap shallowClone() {
final Mindmap result = new Mindmap();
result.setDescription(this.getDescription());
result.setTitle(this.getTitle());
2013-03-30 05:09:28 +01:00
result.setUnzipXml(this.getUnzipXml());
2012-08-17 02:51:59 +02:00
result.setTags(this.getTags());
return result;
}
2012-09-15 16:35:53 +02:00
public boolean hasPermissions(@Nullable Collaborator collaborator, @NotNull CollaborationRole role) {
2012-08-17 02:51:59 +02:00
boolean result = false;
2012-08-26 21:53:33 +02:00
if (collaborator != null) {
final Collaboration collaboration = this.findCollaboration(collaborator);
if (collaboration != null) {
result = collaboration.hasPermissions(role);
}
2012-08-17 02:51:59 +02:00
}
return result;
}
2014-01-29 07:59:05 +01:00
public boolean hasLabel(@NotNull final String name) {
for (Label label : this.labels) {
if (label.getTitle().equals(name)) {
return true;
}
}
return false;
}
2012-08-17 02:51:59 +02:00
}