001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl;
016:
017: import java.util.Iterator;
018:
019: import javax.xml.bind.JAXBException;
020:
021: import com.metaboss.enterprise.ps.PSDataSourceOperationInvocationException;
022: import com.metaboss.enterprise.ps.PSException;
023: import com.metaboss.enterprise.ps.PSUnexpectedProgramConditionException;
024: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSStructure;
025: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STConstraint;
026: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STField;
027: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STStructure;
028: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.ConstraintDefListType;
029: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.ConstraintDefType;
030: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.FieldDefListType;
031: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.FieldDefType;
032: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.StructureDefType;
033:
034: public class PSStructureImpl implements PSStructure {
035: /** Returns details entity corresponding to given reference or null struct if definition not found */
036: public STStructure getStructure(String pStructureRef)
037: throws PSException {
038: StructureDefType lStructureDef = Storage
039: .getStructure(pStructureRef);
040: if (lStructureDef == null)
041: return null;
042: STStructure lStruct = new STStructure();
043: lStruct.StructureRef = lStructureDef.getStructureRef();
044: lStruct.Description = lStructureDef.getDescription();
045: // Good spot to check on validity of fields
046: if (lStruct.StructureRef == null
047: || lStruct.StructureRef.length() == 0)
048: throw new PSUnexpectedProgramConditionException(
049: "StructureRef can not be empty in the definition. StructureRef: "
050: + pStructureRef);
051: if (!lStruct.StructureRef.equals(pStructureRef))
052: throw new PSUnexpectedProgramConditionException(
053: "StructureRef in definition does not match the requested StructureRef. Requested StructureRef: "
054: + pStructureRef
055: + ". Returned StructureRef: "
056: + lStruct.StructureRef);
057: // Check for posible clash with operation result and input structures
058: if (lStruct.StructureRef.toLowerCase().endsWith("result"))
059: throw new PSUnexpectedProgramConditionException(
060: "StructureRef in definition can not end with the word '"
061: + lStruct.StructureRef
062: .substring(lStruct.StructureRef
063: .length() - 6)
064: + "' StructureRef: " + pStructureRef);
065: if (lStruct.StructureRef.toLowerCase().endsWith("input"))
066: throw new PSUnexpectedProgramConditionException(
067: "StructureRef in definition can not end with the word '"
068: + lStruct.StructureRef
069: .substring(lStruct.StructureRef
070: .length() - 5)
071: + "' StructureRef: " + pStructureRef);
072: return lStruct;
073: }
074:
075: /** Inserts one or more new structure records into the database */
076: public void insert(STStructure[] pNewRecords) throws PSException {
077: try {
078: for (int i = 0; i < pNewRecords.length; i++) {
079: STStructure lStructureToInsert = pNewRecords[i];
080: StructureDefType lStructureDef = Util
081: .getObjectFactory().createStructureDef();
082: lStructureDef
083: .setStructureRef(lStructureToInsert.StructureRef);
084: lStructureDef
085: .setDescription(lStructureToInsert.Description);
086: FieldDefListType lFieldDefList = Util
087: .getObjectFactory().createFieldDefList();
088: lStructureDef.setFieldDefList(lFieldDefList);
089: Storage.insertStructure(lStructureDef);
090: }
091: } catch (JAXBException e) {
092: throw new PSDataSourceOperationInvocationException(e);
093: }
094: }
095:
096: /* Returns list of input fields for the operation with the specified name from specified service */
097: public STField[] getFields(String pStructureRef) throws PSException {
098: StructureDefType lStructureDef = Storage
099: .getStructure(pStructureRef);
100: if (lStructureDef == null)
101: throw new PSException(
102: "Structure not found. StructureRef : "
103: + pStructureRef);
104: FieldDefListType lFieldDefList = lStructureDef
105: .getFieldDefList();
106: STField[] lReturn = new STField[lFieldDefList.getFieldDef()
107: .size()];
108: Iterator lIter = lFieldDefList.getFieldDef().iterator();
109: for (int j = 0; lIter.hasNext(); j++) {
110: lReturn[j] = Util.getField((FieldDefType) lIter.next());
111: }
112: return lReturn;
113: }
114:
115: /* Returns list of input Constraints for the operation with the specified name from specified service */
116: public STConstraint[] getConstraints(String pStructureRef)
117: throws PSException {
118: StructureDefType lStructureDef = Storage
119: .getStructure(pStructureRef);
120: if (lStructureDef == null)
121: throw new PSException(
122: "Structure not found. StructureRef : "
123: + pStructureRef);
124: ConstraintDefListType lConstraintDefList = lStructureDef
125: .getConstraintDefList();
126: if (lConstraintDefList != null
127: && lConstraintDefList.getConstraintDef() != null) {
128: STConstraint[] lReturn = new STConstraint[lConstraintDefList
129: .getConstraintDef().size()];
130: Iterator lIter = lConstraintDefList.getConstraintDef()
131: .iterator();
132: for (int j = 0; lIter.hasNext(); j++) {
133: lReturn[j] = Util
134: .getConstraint((ConstraintDefType) lIter.next());
135: }
136: return lReturn;
137: }
138: return null;
139: }
140:
141: /** inserts field into the structure */
142: public void insertField(String pStructureRef, STField pNewField)
143: throws PSException {
144: StructureDefType lStructureDef = Storage
145: .getStructure(pStructureRef);
146: if (lStructureDef == null)
147: throw new PSException(
148: "Structure not found. StructureRef : "
149: + pStructureRef);
150: // Make sure that field with the specified name does not exists yet
151: FieldDefListType lFieldDefList = lStructureDef
152: .getFieldDefList();
153: Iterator lIter = lFieldDefList.getFieldDef().iterator();
154: while (lIter.hasNext()) {
155: FieldDefType lFieldDef = (FieldDefType) lIter.next();
156: if (lFieldDef.getName().equals(pNewField.Name))
157: throw new PSException(
158: "Attempt to insert duplicate field. StructureRef : "
159: + pStructureRef + ". FieldName : "
160: + pNewField.Name);
161: }
162: FieldDefType lNewFieldDef = Util.getFieldDef(pNewField);
163: lFieldDefList.getFieldDef().add(lNewFieldDef);
164: Storage.updateStructure(lStructureDef);
165: }
166: }
|