| 1 | |
package org.talos.impl; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Arrays; |
| 5 | |
import java.util.Collection; |
| 6 | |
import java.util.Collections; |
| 7 | |
import java.util.HashMap; |
| 8 | |
import java.util.HashSet; |
| 9 | |
import java.util.List; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.util.Set; |
| 12 | |
|
| 13 | |
import org.talos.Bouncer; |
| 14 | |
import org.talos.SubjectContainer; |
| 15 | |
import org.talos.exception.TalosHibernateException; |
| 16 | |
import org.talos.impl.repository.Repository; |
| 17 | |
import org.talos.model.Capability; |
| 18 | |
import org.talos.model.Category; |
| 19 | |
import org.talos.model.SecureObject; |
| 20 | |
import org.talos.model.Subject; |
| 21 | |
|
| 22 | 8 | public class SubjectContainerImpl extends AbstractContainer<Subject> implements |
| 23 | |
SubjectContainer { |
| 24 | |
|
| 25 | |
private Repository repository; |
| 26 | |
|
| 27 | |
private boolean subjectOr; |
| 28 | |
|
| 29 | |
public SubjectContainerImpl(Collection<Subject> elements, |
| 30 | |
Repository repository) { |
| 31 | 343 | super(elements); |
| 32 | 343 | this.repository = repository; |
| 33 | 343 | this.subjectOr = false; |
| 34 | 343 | } |
| 35 | |
|
| 36 | |
public SubjectContainerImpl(Collection<Subject> elements, boolean subjectOr, |
| 37 | |
Repository repository) { |
| 38 | 8 | super(elements); |
| 39 | 8 | this.repository = repository; |
| 40 | 8 | this.subjectOr = subjectOr; |
| 41 | 8 | } |
| 42 | |
|
| 43 | |
public SubjectContainerImpl(Subject clone, Repository repository) { |
| 44 | 5 | this(Arrays.asList(clone), repository); |
| 45 | 5 | } |
| 46 | |
|
| 47 | |
public Bouncer andObject(String... objectIds) { |
| 48 | 57 | Collection<SecureObject> objects = new HashSet<SecureObject>(); |
| 49 | |
|
| 50 | 142 | for (String id : objectIds) { |
| 51 | 86 | SecureObject loadedObject = repository.load(SecureObject.class, id); |
| 52 | 85 | objects.add(loadedObject); |
| 53 | |
} |
| 54 | 56 | return new ObjectBouncer(this.elements.values(), objects, this.subjectOr, repository); |
| 55 | |
} |
| 56 | |
|
| 57 | |
public Bouncer andCategory(String... categoryIds) { |
| 58 | 57 | Collection<Category> categories = new HashSet<Category>(); |
| 59 | |
|
| 60 | 151 | for (String id : categoryIds) { |
| 61 | 95 | Category loadedCategory = repository.load(Category.class, id); |
| 62 | 94 | categories.add(loadedCategory); |
| 63 | |
} |
| 64 | 56 | return new CategoryBouncer(this.elements.values(), categories, this.subjectOr, |
| 65 | |
repository); |
| 66 | |
} |
| 67 | |
|
| 68 | |
public SubjectContainer cloneAs(String name) { |
| 69 | 7 | checkCloneAllowed(); |
| 70 | |
try { |
| 71 | 6 | Subject clone = new Subject(name); |
| 72 | 5 | repository.create(clone); |
| 73 | 3 | for (Capability cap : repository.loadAllCapabilities(this |
| 74 | |
.getFirstElement())) { |
| 75 | 6 | Capability copy = cap.copy(clone); |
| 76 | 6 | repository.save(copy); |
| 77 | 6 | } |
| 78 | 3 | return new SubjectContainerImpl(clone, this.repository); |
| 79 | 1 | } catch (TalosHibernateException e) { |
| 80 | 1 | repository.rollback(); |
| 81 | 1 | throw e; |
| 82 | |
} |
| 83 | |
} |
| 84 | |
|
| 85 | |
@Override |
| 86 | |
protected void remove(Subject element) { |
| 87 | 8 | Collection<Capability> capabilities = repository |
| 88 | |
.loadAllCapabilities(element); |
| 89 | 8 | for (Capability capability : capabilities) { |
| 90 | 11 | repository.delete(capability); |
| 91 | |
} |
| 92 | 8 | repository.delete(element); |
| 93 | 8 | } |
| 94 | |
|
| 95 | |
public Bouncer andObject(Collection<SecureObject> secureObjects) { |
| 96 | 5 | if (secureObjects.size() > 0) { |
| 97 | 4 | return new ObjectBouncer(this.elements.values(), secureObjects, this.subjectOr, |
| 98 | |
repository); |
| 99 | |
} else { |
| 100 | 1 | throw new IllegalArgumentException("secureObjects can't be empty"); |
| 101 | |
} |
| 102 | |
} |
| 103 | |
|
| 104 | |
public Collection<Category> listCategories(){ |
| 105 | 3 | Map<Subject, Collection<Category>> values = new HashMap<Subject, Collection<Category>>(); |
| 106 | 3 | Set<Category> result = new HashSet<Category>(); |
| 107 | 3 | for(Subject s : this.list()){ |
| 108 | 6 | values.put(s, repository.listCategories(s)); |
| 109 | |
} |
| 110 | 3 | if(this.subjectOr){ |
| 111 | 1 | for(Subject s : values.keySet()){ |
| 112 | 2 | result.addAll(values.get(s)); |
| 113 | |
} |
| 114 | |
} else { |
| 115 | 2 | Set<Subject> subjects = values.keySet(); |
| 116 | 2 | Set<Subject> toCompare = values.keySet(); |
| 117 | |
|
| 118 | 2 | for(Subject s : subjects){ |
| 119 | 4 | boolean containsAll = true; |
| 120 | 4 | for(Subject compare : toCompare){ |
| 121 | 7 | if(!values.get(s).equals(values.get(compare))){ |
| 122 | 2 | if(!values.get(s).containsAll(values.get(compare)) |
| 123 | |
|| !values.get(compare).containsAll(values.get(s))){ |
| 124 | 2 | containsAll = false; |
| 125 | 2 | break; |
| 126 | |
} |
| 127 | |
} |
| 128 | |
} |
| 129 | 4 | if(containsAll){ |
| 130 | 2 | result.addAll(values.get(s)); |
| 131 | |
} |
| 132 | 4 | } |
| 133 | |
} |
| 134 | 3 | List<Category> res = new ArrayList<Category>(result); |
| 135 | 3 | Collections.sort(res); |
| 136 | 3 | return res; |
| 137 | |
} |
| 138 | |
|
| 139 | |
public Bouncer andCategory(Collection<Category> categories) { |
| 140 | 3 | if (categories!=null) { |
| 141 | 3 | return new CategoryBouncer(this.elements.values(), categories, this.subjectOr, |
| 142 | |
repository); |
| 143 | |
} else { |
| 144 | 0 | throw new IllegalArgumentException("categories can't be null"); |
| 145 | |
} |
| 146 | |
} |
| 147 | |
} |