001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org . |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013:
014: package org.rapla.storage.xml;
015:
016: import java.io.IOException;
017: import java.util.Iterator;
018:
019: import org.rapla.entities.Category;
020: import org.rapla.entities.EntityNotFoundException;
021: import org.rapla.entities.RaplaObject;
022: import org.rapla.entities.domain.Allocatable;
023: import org.rapla.entities.domain.Permission;
024: import org.rapla.entities.internal.CategoryImpl;
025: import org.rapla.framework.RaplaContext;
026: import org.rapla.framework.RaplaException;
027:
028: public class AllocatableWriter extends ClassifiableWriter {
029: public AllocatableWriter(RaplaContext sm) throws RaplaException {
030: super (sm);
031: }
032:
033: public void printAllocatable(Allocatable allocatable)
034: throws IOException, RaplaException {
035: String tagName = allocatable.isPerson() ? "rapla:person"
036: : "rapla:resource";
037: openTag(tagName);
038: printId(allocatable);
039: printVersion(allocatable);
040: if (allocatable.isHoldBackConflicts()) {
041: att("holdbackconflicts", "true");
042: }
043: closeTag();
044: printClassification(allocatable.getClassification());
045:
046: Permission[] permissions = allocatable.getPermissions();
047: for (int i = 0; i < permissions.length; i++) {
048: printPermission(permissions[i]);
049: }
050: closeElement(tagName);
051: }
052:
053: public void writeObject(RaplaObject object) throws IOException,
054: RaplaException {
055: printAllocatable((Allocatable) object);
056: }
057:
058: protected void printPermission(Permission p) throws IOException,
059: RaplaException {
060: openTag("rapla:permission");
061: if (p.getUser() != null) {
062: att("user", getId(p.getUser()));
063: } else if (p.getGroup() != null) {
064: if (isIdOnly()) {
065: att("groupidref", getId(p.getGroup()));
066: } else {
067: att("group", getGroupPath(p.getGroup()));
068: }
069: }
070: if (p.getMinAdvance() != null) {
071: att("min-advance", p.getMinAdvance().toString());
072: }
073: if (p.getMaxAdvance() != null) {
074: att("max-advance", p.getMaxAdvance().toString());
075: }
076: if (p.getStart() != null) {
077: att("start-date", dateTimeFormat.formatDate(p.getStart()));
078: }
079: if (p.getEnd() != null) {
080: att("end-date", dateTimeFormat.formatDate(p.getEnd()));
081: }
082: if (p.getAccessLevel() != Permission.ALLOCATE_CONFLICTS) {
083: att("access", (String) Permission.ACCESS_LEVEL_NAMEMAP
084: .get(p.getAccessLevel()));
085: }
086: closeElementTag();
087: }
088:
089: private String getGroupPath(Category category)
090: throws EntityNotFoundException {
091: Category rootCategory = cache.getSuperCategory().getCategory(
092: Permission.GROUP_CATEGORY_KEY);
093: return ((CategoryImpl) rootCategory)
094: .getPathForCategory(category);
095: }
096:
097: public void printAllocatables() throws IOException, RaplaException {
098: openElement("rapla:resources");
099: println("<!-- resources -->");
100: // Print all resources that are not persons
101: Iterator it = cache.getIterator(Allocatable.TYPE);
102: while (it.hasNext()) {
103: Allocatable allocatable = (Allocatable) it.next();
104: if (allocatable.isPerson())
105: continue;
106: printAllocatable(allocatable);
107: }
108: // Print all Persons
109: it = cache.getIterator(Allocatable.TYPE);
110: while (it.hasNext()) {
111: Allocatable allocatable = (Allocatable) it.next();
112: if (!allocatable.isPerson())
113: continue;
114: printAllocatable(allocatable);
115: }
116: println();
117: closeElement("rapla:resources");
118:
119: }
120:
121: }
|