001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.workflow.commonimpl;
017:
018: import org.outerj.daisy.workflow.WfPool;
019: import org.outerj.daisy.repository.Repository;
020: import org.outerj.daisy.repository.RepositoryException;
021: import org.outerx.daisy.x10Workflow.PoolDocument;
022:
023: import java.util.Date;
024:
025: public class WfPoolImpl implements WfPool {
026: private long id = -1;
027: private String name;
028: private String description;
029: private long lastModifier;
030: private Date lastModified;
031: private long updateCount = 0;
032: private WfPoolStrategy strategy;
033: private Repository repository;
034: private boolean readOnly = false;
035: private IntimateAccess intimateAccess = new IntimateAccess();
036: private static final String READONLY_MESSAGE = "This workflow pool object is readonly.";
037:
038: public WfPoolImpl(String name, WfPoolStrategy strategy,
039: Repository repository) {
040: checkName(name);
041: this .name = name;
042: this .strategy = strategy;
043: this .repository = repository;
044: }
045:
046: public IntimateAccess getIntimateAccess(WfPoolStrategy strategy) {
047: if (readOnly)
048: throw new RuntimeException(READONLY_MESSAGE);
049:
050: if (this .strategy == strategy)
051: return intimateAccess;
052: else
053: return null;
054: }
055:
056: private void checkName(String name) {
057: if (name == null)
058: throw new IllegalArgumentException("Null argument: name");
059:
060: if (name.trim().length() == 0)
061: throw new IllegalArgumentException(
062: "empty or all-whitespace pool name");
063: }
064:
065: public long getId() {
066: return id;
067: }
068:
069: public String getName() {
070: return name;
071: }
072:
073: public void setName(String name) {
074: if (readOnly)
075: throw new RuntimeException(READONLY_MESSAGE);
076:
077: checkName(name);
078: this .name = name;
079: }
080:
081: public String getDescription() {
082: return description;
083: }
084:
085: public void setDescription(String description) {
086: if (readOnly)
087: throw new RuntimeException(READONLY_MESSAGE);
088:
089: this .description = description;
090: }
091:
092: public void save() throws RepositoryException {
093: if (readOnly)
094: throw new RuntimeException(READONLY_MESSAGE);
095:
096: strategy.store(this , repository);
097: }
098:
099: public Date getLastModified() {
100: return lastModified != null ? (Date) lastModified.clone()
101: : null;
102: }
103:
104: public long getLastModifier() {
105: return lastModifier;
106: }
107:
108: public long getUpdateCount() {
109: return updateCount;
110: }
111:
112: public void makeReadOnly() {
113: this .readOnly = true;
114: }
115:
116: public PoolDocument getXml() {
117: PoolDocument document = PoolDocument.Factory.newInstance();
118: PoolDocument.Pool xml = document.addNewPool();
119:
120: xml.setName(name);
121: if (description != null)
122: xml.setDescription(description);
123:
124: if (id != -1) {
125: xml.setId(id);
126: xml.setLastModified(WfXmlHelper.getCalendar(lastModified));
127: xml.setLastModifier(lastModifier);
128: xml.setUpdateCount(updateCount);
129: }
130:
131: return document;
132: }
133:
134: public void setAllFromXml(PoolDocument.Pool poolXml) {
135: if (readOnly)
136: throw new RuntimeException(READONLY_MESSAGE);
137:
138: this .name = poolXml.getName();
139: this .description = poolXml.getDescription();
140: }
141:
142: public class IntimateAccess {
143: private IntimateAccess() {
144: // private on purpose
145: }
146:
147: public Repository getRepository() {
148: return repository;
149: }
150:
151: public void setId(long id) {
152: if (readOnly)
153: throw new RuntimeException(READONLY_MESSAGE);
154: WfPoolImpl.this .id = id;
155: }
156:
157: public void setLastModified(Date lastModified) {
158: if (readOnly)
159: throw new RuntimeException(READONLY_MESSAGE);
160: WfPoolImpl.this .lastModified = lastModified;
161: }
162:
163: public void setLastModifier(long lastModifier) {
164: if (readOnly)
165: throw new RuntimeException(READONLY_MESSAGE);
166: WfPoolImpl.this .lastModifier = lastModifier;
167: }
168:
169: public void setUpdateCount(long updateCount) {
170: if (readOnly)
171: throw new RuntimeException(READONLY_MESSAGE);
172: WfPoolImpl.this.updateCount = updateCount;
173: }
174: }
175: }
|