summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/data
diff options
context:
space:
mode:
authorsnowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb>2011-03-19 22:39:51 +0000
committersnowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb>2011-03-19 22:39:51 +0000
commit59d0415c7d7c1aa1a1720734d73f9aada9c38c89 (patch)
tree751109b805d814fb4d290ce5b03fb19dc52f11f2 /EssentialsGroupManager/src/org/anjocaido/groupmanager/data
parentea668bf9d0733ad1ec6dd0653b1d243b3d979a59 (diff)
downloadEssentials-59d0415c7d7c1aa1a1720734d73f9aada9c38c89.tar
Essentials-59d0415c7d7c1aa1a1720734d73f9aada9c38c89.tar.gz
Essentials-59d0415c7d7c1aa1a1720734d73f9aada9c38c89.tar.lz
Essentials-59d0415c7d7c1aa1a1720734d73f9aada9c38c89.tar.xz
Essentials-59d0415c7d7c1aa1a1720734d73f9aada9c38c89.zip
Moving all files to trunk.
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@969 e251c2fe-e539-e718-e476-b85c1f46cddb
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/data')
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java114
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java122
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java87
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java187
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java45
-rw-r--r--EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java192
6 files changed, 747 insertions, 0 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java
new file mode 100644
index 000000000..3eb7491dd
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/DataUnit.java
@@ -0,0 +1,114 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import org.anjocaido.groupmanager.GroupManager;
+import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
+import org.anjocaido.groupmanager.utils.StringPermissionComparator;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public abstract class DataUnit {
+
+ private WorldDataHolder dataSource;
+ private String name;
+ private boolean changed;
+ private ArrayList<String> permissions = new ArrayList<String>();
+
+ public DataUnit(WorldDataHolder dataSource, String name) {
+ this.dataSource = dataSource;
+ this.name = name;
+ }
+
+ /**
+ * Every group is matched only by their names and DataSources names.
+ * @param o
+ * @return true if they are equal. false if not.
+ */
+ @Override
+ public boolean equals(Object o) {
+ if (o instanceof DataUnit) {
+ DataUnit go = (DataUnit) o;
+ if (this.getName().equalsIgnoreCase(go.getName()) && this.dataSource.getName().equalsIgnoreCase(go.getDataSource().getName())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = 5;
+ hash = 71 * hash + (this.name != null ? this.name.toLowerCase().hashCode() : 0);
+ return hash;
+ }
+
+
+
+
+ /**
+ * @return the dataSource
+ */
+ public WorldDataHolder getDataSource() {
+ return dataSource;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ public void flagAsChanged() {
+ GroupManager.logger.finest("DataSource: "+getDataSource().getName()+" - DataUnit: "+getName()+" flagged as changed!");
+// for(StackTraceElement st: Thread.currentThread().getStackTrace()){
+// GroupManager.logger.finest(st.toString());
+// }
+ changed = true;
+ }
+
+ public boolean isChanged() {
+ return changed;
+ }
+
+ public void flagAsSaved() {
+ GroupManager.logger.finest("DataSource: "+getDataSource().getName()+" - DataUnit: "+getName()+" flagged as saved!");
+ changed = false;
+ }
+
+ public boolean hasSamePermissionNode(String permission) {
+ return permissions.contains(permission);
+ }
+
+ public void addPermission(String permission) {
+ if (!hasSamePermissionNode(permission)) {
+ permissions.add(permission);
+ }
+ flagAsChanged();
+ }
+
+ public boolean removePermission(String permission) {
+ flagAsChanged();
+ return permissions.remove(permission);
+ }
+
+ /**
+ * Use this only to list permissions.
+ * You can't edit the permissions using the returned ArrayList instance
+ * @return a copy of the permission list
+ */
+ public ArrayList<String> getPermissionList() {
+ return (ArrayList<String>) permissions.clone();
+ }
+
+ public void sortPermissions(){
+ Collections.sort(permissions, StringPermissionComparator.getInstance());
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java
new file mode 100644
index 000000000..c734b208e
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Group.java
@@ -0,0 +1,122 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
+import java.util.ArrayList;
+import java.util.Map;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public class Group extends DataUnit implements Cloneable {
+
+ /**
+ * The group it inherits DIRECTLY!
+ */
+ private ArrayList<String> inherits = new ArrayList<String>();
+ /**
+ *This one holds the fields in INFO node.
+ * like prefix = 'c'
+ * or build = false
+ */
+ private GroupVariables variables = new GroupVariables(this);
+
+ /**
+ *
+ * @param name
+ */
+ public Group(WorldDataHolder source, String name) {
+ super(source,name);
+ }
+
+ /**
+ * Clone this group
+ * @return a clone of this group
+ */
+ @Override
+ public Group clone() {
+ Group clone = new Group(getDataSource(), this.getName());
+ clone.inherits = ((ArrayList<String>) this.getInherits().clone());
+ for(String perm: this.getPermissionList()){
+ clone.addPermission(perm);
+ }
+ clone.variables = ((GroupVariables) variables).clone(clone);
+ //clone.flagAsChanged();
+ return clone;
+ }
+
+ /**
+ * Use this to deliver a group from a different dataSource to another
+ * @param dataSource
+ * @return
+ */
+ public Group clone(WorldDataHolder dataSource) {
+ if (dataSource.groupExists(this.getName())) {
+ return null;
+ }
+ Group clone = getDataSource().createGroup(this.getName());
+ clone.inherits = ((ArrayList<String>) this.getInherits().clone());
+ for(String perm: this.getPermissionList()){
+ clone.addPermission(perm);
+ }
+ clone.variables = variables.clone(clone);
+ clone.flagAsChanged(); //use this to make the new dataSource save the new group
+ return clone;
+ }
+
+ /**
+ * a COPY of inherits list
+ * You can't manage the list by here
+ * Lol... version 0.6 had a problem because this.
+ * @return the inherits
+ */
+ public ArrayList<String> getInherits() {
+ return (ArrayList<String>) inherits.clone();
+ }
+
+ /**
+ * @param inherits the inherits to set
+ */
+ public void addInherits(Group inherit) {
+ if (!this.getDataSource().groupExists(inherit.getName())) {
+ getDataSource().addGroup(inherit);
+ }
+ if (!inherits.contains(inherit.getName().toLowerCase())) {
+ inherits.add(inherit.getName().toLowerCase());
+ }
+ flagAsChanged();
+ }
+
+ public boolean removeInherits(String inherit) {
+ if (this.inherits.contains(inherit.toLowerCase())) {
+ this.inherits.remove(inherit.toLowerCase());
+ flagAsChanged();
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @return the variables
+ */
+ public GroupVariables getVariables() {
+ return variables;
+ }
+
+ /**
+ *
+ * @param varList
+ */
+ public void setVariables(Map<String, Object> varList) {
+ GroupVariables temp = new GroupVariables(this, varList);
+ variables.clearVars();
+ for(String key: temp.getVarKeyList()){
+ variables.addVar(key, temp.getVarObject(key));
+ }
+ flagAsChanged();
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java
new file mode 100644
index 000000000..87c34de2b
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/GroupVariables.java
@@ -0,0 +1,87 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import java.util.Map;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public class GroupVariables extends Variables implements Cloneable {
+
+ private Group owner;
+
+ public GroupVariables(Group owner) {
+ super(owner);
+ this.owner = owner;
+ addVar("prefix", "");
+ addVar("suffix", "");
+ addVar("build", false);
+ }
+
+ public GroupVariables(Group owner, Map<String, Object> varList) {
+ super(owner);
+ variables = varList;
+ if (variables.get("prefix") == null) {
+ variables.put("prefix", "");
+ owner.flagAsChanged();
+ }
+ //thisGrp.prefix = infoNode.get("prefix").toString();
+
+ if (variables.get("suffix") == null) {
+ variables.put("suffix", "");
+ owner.flagAsChanged();
+ }
+ //thisGrp.suffix = infoNode.get("suffix").toString();
+
+ if (variables.get("build") == null) {
+ variables.put("build", false);
+ owner.flagAsChanged();
+ }
+ this.owner = owner;
+ }
+
+ /**
+ * A clone of all vars here.
+ * @return
+ */
+ protected GroupVariables clone(Group newOwner) {
+ GroupVariables clone = new GroupVariables(newOwner);
+ for (String key : variables.keySet()) {
+ clone.variables.put(key, variables.get(key));
+ }
+ newOwner.flagAsChanged();
+ return clone;
+ }
+
+ /**
+ * Remove a var from the list
+ * @param name
+ */
+ @Override
+ public void removeVar(String name) {
+ try {
+ this.variables.remove(name);
+ } catch (Exception e) {
+ }
+ if (name.equals("prefix")) {
+ addVar("prefix", "");
+ } else if (name.equals("suffix")) {
+ addVar("suffix", "");
+ } else if (name.equals("build")) {
+ addVar("build", false);
+ }
+ owner.flagAsChanged();
+ }
+
+ /**
+ * @return the owner
+ */
+ @Override
+ public Group getOwner() {
+ return owner;
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java
new file mode 100644
index 000000000..ceecbfa21
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/User.java
@@ -0,0 +1,187 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import com.sun.org.apache.bcel.internal.generic.AALOAD;
+import java.util.ArrayList;
+import org.anjocaido.groupmanager.dataholder.WorldDataHolder;
+import java.util.Map;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public class User extends DataUnit implements Cloneable {
+
+ /**
+ *
+ */
+ private String group = null;
+ private ArrayList<String> subGroups = new ArrayList<String>();
+ /**
+ *This one holds the fields in INFO node.
+ * like prefix = 'c'
+ * or build = false
+ */
+ private UserVariables variables = new UserVariables(this);
+
+
+ /**
+ *
+ * @param name
+ */
+ public User(WorldDataHolder source, String name) {
+ super(source,name);
+ this.group = source.getDefaultGroup().getName();
+ }
+
+ /**
+ *
+ * @return
+ */
+ @Override
+ public User clone() {
+ User clone = new User(getDataSource(), this.getName());
+ clone.group = this.group;
+ for(String perm: this.getPermissionList()){
+ clone.addPermission(perm);
+ }
+ //clone.variables = this.variables.clone();
+ //clone.flagAsChanged();
+ return clone;
+ }
+
+ /**
+ * Use this to deliver a user from one WorldDataHolder to another
+ * @param dataSource
+ * @return null if given dataSource already contains the same user
+ */
+ public User clone(WorldDataHolder dataSource) {
+ if (dataSource.isUserDeclared(this.getName())) {
+ return null;
+ }
+ User clone = dataSource.createUser(this.getName());
+ if (dataSource.getGroup(group) == null) {
+ clone.setGroup(dataSource.getDefaultGroup());
+ } else {
+ clone.setGroup(this.getGroupName());
+ }
+ for(String perm: this.getPermissionList()){
+ clone.addPermission(perm);
+ }
+ //clone.variables = this.variables.clone();
+ clone.flagAsChanged();
+ return clone;
+ }
+
+ public Group getGroup() {
+ Group result = getDataSource().getGroup(group);
+ if (result == null) {
+ this.setGroup(getDataSource().getDefaultGroup());
+ result = getDataSource().getDefaultGroup();
+ }
+ return result;
+ }
+
+ /**
+ * @return the group
+ */
+ public String getGroupName() {
+ Group result = getDataSource().getGroup(group);
+ if (result == null) {
+ group = getDataSource().getDefaultGroup().getName();
+ }
+ return group;
+ }
+
+ /**
+ * @param group the group to set
+ */
+ @Deprecated
+ public void setGroup(String group) {
+ this.group = group;
+ flagAsChanged();
+ }
+
+ /**
+ * @param group the group to set
+ */
+ public void setGroup(Group group) {
+ if (!this.getDataSource().groupExists(group.getName())) {
+ getDataSource().addGroup(group);
+ }
+ group = getDataSource().getGroup(group.getName());
+ this.group = group.getName();
+ flagAsChanged();
+ }
+
+ public void addSubGroup(Group subGroup){
+ if(this.group.equalsIgnoreCase(subGroup.getName())){
+ return;
+ }
+ if (!this.getDataSource().groupExists(subGroup.getName())) {
+ getDataSource().addGroup(subGroup);
+ }
+ subGroup = getDataSource().getGroup(subGroup.getName());
+ removeSubGroup(subGroup);
+ subGroups.add(subGroup.getName());
+ flagAsChanged();
+ }
+ public int subGroupsSize(){
+ return subGroups.size();
+ }
+ public boolean isSubGroupsEmpty(){
+ return subGroups.isEmpty();
+ }
+ public boolean containsSubGroup(Group subGroup){
+ return subGroups.contains(subGroup.getName());
+ }
+ public boolean removeSubGroup(Group subGroup){
+ try{
+ if(subGroups.remove(subGroup.getName())){
+ flagAsChanged();
+ return true;
+ }
+ } catch (Exception e){
+
+ }
+ return false;
+ }
+ public ArrayList<Group> subGroupListCopy(){
+ ArrayList<Group> val = new ArrayList<Group>();
+ for(String gstr: subGroups){
+ Group g = getDataSource().getGroup(gstr);
+ if(g==null){
+ removeSubGroup(g);
+ continue;
+ }
+ val.add(g);
+ }
+ return val;
+ }
+ public ArrayList<String> subGroupListStringCopy(){
+ return (ArrayList<String>) subGroups.clone();
+ }
+
+ /**
+ * @return the variables
+ */
+ public UserVariables getVariables() {
+ return variables;
+ }
+
+ /**
+ *
+ * @param varList
+ */
+ public void setVariables(Map<String, Object> varList) {
+ UserVariables temp = new UserVariables(this, varList);
+ variables.clearVars();
+ for(String key: temp.getVarKeyList()){
+ variables.addVar(key, temp.getVarObject(key));
+ }
+ flagAsChanged();
+ }
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java
new file mode 100644
index 000000000..45dfc31e7
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/UserVariables.java
@@ -0,0 +1,45 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.anjocaido.groupmanager.data;
+
+import java.util.Map;
+
+/**
+ *
+ * @author gabrielcouto
+ */
+public class UserVariables extends Variables{
+ private User owner;
+ public UserVariables(User owner){
+ super(owner);
+ this.owner = owner;
+ }
+ public UserVariables(User owner, Map<String, Object> varList) {
+ super(owner);
+ this.variables = varList;
+ this.owner = owner;
+ }
+ /**
+ * A clone of all vars here.
+ * @return
+ */
+ protected UserVariables clone(User newOwner) {
+ UserVariables clone = new UserVariables(newOwner);
+ for (String key : variables.keySet()) {
+ clone.variables.put(key, variables.get(key));
+ }
+ newOwner.flagAsChanged();
+ return clone;
+ }
+ /**
+ * @return the owner
+ */
+ @Override
+ public User getOwner() {
+ return owner;
+ }
+
+}
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java
new file mode 100644
index 000000000..31ed0d795
--- /dev/null
+++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/data/Variables.java
@@ -0,0 +1,192 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anjocaido.groupmanager.data;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ *A class that holds variables of a user/group.
+ * In groups, it holds the contents of INFO node.
+ * Like:
+ * prefix
+ * suffix
+ * build
+ *
+ * @author gabrielcouto
+ */
+public abstract class Variables implements Cloneable {
+ private DataUnit owner;
+ protected Map<String, Object> variables = new HashMap<String, Object>();
+
+ public Variables(DataUnit owner){
+ this.owner = owner;
+ }
+ /**
+ * Add var to the the INFO node.
+ * examples:
+ * addVar("build",true);
+ * addVar("prefix","c");
+ * @param name key name of the var
+ * @param o the object value of the var
+ */
+ public void addVar(String name, Object o) {
+ if (o == null) {
+ return;
+ }
+ if (variables.containsKey(name)) {
+ variables.remove(name);
+ }
+ variables.put(name, o);
+ owner.flagAsChanged();
+ }
+
+ /**
+ * Returns the object inside the var
+ * @param name
+ * @return a Object if exists. null if doesn't exists
+ */
+ public Object getVarObject(String name) {
+ return variables.get(name);
+ }
+
+ /**
+ * Get the String value for the given var name
+ * @param name the var key name
+ * @return "" if null. or the toString() value of object
+ */
+ public String getVarString(String name) {
+ Object o = variables.get(name);
+ try {
+ return o == null ? "" : o.toString();
+ } catch (Exception e) {
+ return "";
+ }
+ }
+
+ /**
+ *
+ * @param name
+ * @return false if null. or a Boolean.parseBoolean of the string
+ */
+ public Boolean getVarBoolean(String name) {
+ Object o = variables.get(name);
+ try {
+ return o == null ? false : Boolean.parseBoolean(o.toString());
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /**
+ *
+ * @param name
+ * @return -1 if null. or a parseInt of the string
+ */
+ public Integer getVarInteger(String name) {
+ Object o = variables.get(name);
+ try {
+ return o == null ? -1 : Integer.parseInt(o.toString());
+ } catch (Exception e) {
+ return -1;
+ }
+ }
+
+ /**
+ *
+ * @param name
+ * @return -1 if null. or a parseDouble of the string
+ */
+ public Double getVarDouble(String name) {
+ Object o = variables.get(name);
+ try {
+ return o == null ? -1.0D : Double.parseDouble(o.toString());
+ } catch (Exception e) {
+ return -1.0D;
+
+
+ }
+ }
+
+ /**
+ * All variable keys this is holding
+ * @return
+ */
+ public Set<String> getVarKeyList() {
+ return variables.keySet();
+
+
+ }
+
+ /**
+ * verify is a var exists
+ * @param name the key name of the var
+ * @return true if that var exists
+ */
+ public boolean hasVar(String name) {
+ return variables.containsKey(name);
+
+
+ }
+
+ /**
+ * Returns the quantity of vars this is holding
+ * @return the number of vars
+ */
+ public int getSize() {
+ return variables.size();
+
+
+ }
+
+ /**
+ * Remove a var from the list
+ * @param name
+ */
+ public void removeVar(String name) {
+ try {
+ variables.remove(name);
+ } catch (Exception e) {
+ }
+ owner.flagAsChanged();
+ }
+
+ public static Object parseVariableValue(String value) {
+ try {
+ Integer i = Integer.parseInt(value);
+ return i;
+ } catch (NumberFormatException e) {
+ }
+ try {
+ Double d = Double.parseDouble(value);
+ return d;
+ } catch (NumberFormatException e) {
+ }
+ if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("on")) {
+ return true;
+ } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("off")) {
+ return false;
+ }
+ return value;
+
+ }
+
+ public void clearVars(){
+ variables.clear();
+ owner.flagAsChanged();
+ }
+
+ /**
+ * @return the owner
+ */
+ public DataUnit getOwner() {
+ return owner;
+ }
+
+ public boolean isEmpty(){
+ return variables.isEmpty();
+ }
+}