0001: /***************************************************************
0002: * This file is part of the [fleXive](R) project.
0003: *
0004: * Copyright (c) 1999-2008
0005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
0006: * All rights reserved
0007: *
0008: * The [fleXive](R) project is free software; you can redistribute
0009: * it and/or modify it under the terms of the GNU General Public
0010: * License as published by the Free Software Foundation;
0011: * either version 2 of the License, or (at your option) any
0012: * later version.
0013: *
0014: * The GNU General Public License can be found at
0015: * http://www.gnu.org/copyleft/gpl.html.
0016: * A copy is found in the textfile GPL.txt and important notices to the
0017: * license from the author are found in LICENSE.txt distributed with
0018: * these libraries.
0019: *
0020: * This library is distributed in the hope that it will be useful,
0021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0023: * GNU General Public License for more details.
0024: *
0025: * For further information about UCS - unique computing solutions gmbh,
0026: * please see the company website: http://www.ucs.at
0027: *
0028: * For further information about [fleXive](R), please see the
0029: * project website: http://www.flexive.org
0030: *
0031: *
0032: * This copyright notice MUST APPEAR in all copies of the file!
0033: ***************************************************************/package com.flexive.core.structure;
0034:
0035: import com.flexive.shared.FxArrayUtils;
0036: import com.flexive.shared.FxContext;
0037: import com.flexive.shared.XPathElement;
0038: import com.flexive.shared.exceptions.FxInvalidParameterException;
0039: import com.flexive.shared.exceptions.FxNotFoundException;
0040: import com.flexive.shared.exceptions.FxRuntimeException;
0041: import com.flexive.shared.scripting.FxScriptInfo;
0042: import com.flexive.shared.scripting.FxScriptMapping;
0043: import com.flexive.shared.security.ACL;
0044: import com.flexive.shared.security.Mandator;
0045: import com.flexive.shared.security.UserTicket;
0046: import com.flexive.shared.structure.*;
0047: import com.flexive.shared.workflow.Route;
0048: import com.flexive.shared.workflow.Step;
0049: import com.flexive.shared.workflow.StepDefinition;
0050: import com.flexive.shared.workflow.Workflow;
0051:
0052: import java.util.ArrayList;
0053: import java.util.Collections;
0054: import java.util.List;
0055:
0056: /**
0057: * Runtime object for environment metadata held in the cache.
0058: *
0059: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
0060: */
0061: public final class FxEnvironmentImpl implements FxEnvironment {
0062: private static final long serialVersionUID = 7107237825721203341L;
0063:
0064: private List<FxDataType> dataTypes;
0065: private List<ACL> acls;
0066: private List<Workflow> workflows;
0067: private List<FxSelectList> selectLists;
0068: private List<FxGroup> groups;
0069: private List<FxProperty> properties;
0070: private List<FxPropertyAssignment> propertyAssignmentsEnabled;
0071: private List<FxPropertyAssignment> propertyAssignmentsAll;
0072: private List<FxPropertyAssignment> propertyAssignmentsSystemInternalRoot;
0073: private List<FxGroupAssignment> groupAssignmentsEnabled;
0074: private List<FxGroupAssignment> groupAssignmentsAll;
0075: private List<FxType> types;
0076: private Mandator[] mandators;
0077: private String inactiveMandators = null;
0078: private String deactivatedTypes = null;
0079: private List<FxAssignment> assignments;
0080: private List<StepDefinition> stepDefinitions;
0081: private List<Step> steps;
0082: private List<FxScriptInfo> scripts;
0083: private List<FxScriptMapping> scriptMappings;
0084: private long timeStamp = 0;
0085:
0086: public FxEnvironmentImpl() {
0087: }
0088:
0089: /**
0090: * Copy constructor
0091: *
0092: * @param e source
0093: */
0094: private FxEnvironmentImpl(FxEnvironmentImpl e) {
0095: this .dataTypes = new ArrayList<FxDataType>(e.dataTypes);
0096: this .acls = new ArrayList<ACL>(e.acls);
0097: this .workflows = new ArrayList<Workflow>(e.workflows);
0098: this .groups = new ArrayList<FxGroup>(e.groups);
0099: this .properties = new ArrayList<FxProperty>(e.properties);
0100: this .propertyAssignmentsEnabled = new ArrayList<FxPropertyAssignment>(
0101: e.propertyAssignmentsEnabled);
0102: this .propertyAssignmentsAll = new ArrayList<FxPropertyAssignment>(
0103: e.propertyAssignmentsAll);
0104: this .propertyAssignmentsSystemInternalRoot = new ArrayList<FxPropertyAssignment>(
0105: e.propertyAssignmentsSystemInternalRoot);
0106: this .groupAssignmentsEnabled = new ArrayList<FxGroupAssignment>(
0107: e.groupAssignmentsEnabled);
0108: this .groupAssignmentsAll = new ArrayList<FxGroupAssignment>(
0109: e.groupAssignmentsAll);
0110: this .types = new ArrayList<FxType>(e.types);
0111: this .mandators = new Mandator[e.mandators.length];
0112: System.arraycopy(e.mandators, 0, this .mandators, 0,
0113: mandators.length);
0114: this .assignments = new ArrayList<FxAssignment>(e.assignments);
0115: this .stepDefinitions = new ArrayList<StepDefinition>(
0116: e.stepDefinitions);
0117: this .steps = new ArrayList<Step>(e.steps);
0118: if (e.scripts != null) {
0119: this .scripts = new ArrayList<FxScriptInfo>(e.scripts);
0120: this .scriptMappings = new ArrayList<FxScriptMapping>(
0121: e.scriptMappings);
0122: }
0123: this .selectLists = new ArrayList<FxSelectList>(e.selectLists);
0124: this .timeStamp = e.timeStamp;
0125: }
0126:
0127: /**
0128: * Assignment of all known FxDataType
0129: *
0130: * @param dataTypes all known data types
0131: */
0132: protected void setDataTypes(List<FxDataType> dataTypes) {
0133: this .dataTypes = dataTypes;
0134: }
0135:
0136: /**
0137: * Assign all defined ACL's
0138: *
0139: * @param acls all defined ALC's
0140: */
0141: protected void setAcls(List<ACL> acls) {
0142: this .acls = acls;
0143: }
0144:
0145: /**
0146: * Assign all step definitions
0147: *
0148: * @param stepDefinitions all step definitions
0149: */
0150: protected void setStepDefinitions(
0151: List<StepDefinition> stepDefinitions) {
0152: this .stepDefinitions = stepDefinitions;
0153: }
0154:
0155: /**
0156: * {@inheritDoc}
0157: */
0158: public List<StepDefinition> getStepDefinitions() {
0159: return Collections.unmodifiableList(stepDefinitions);
0160: }
0161:
0162: /**
0163: * {@inheritDoc}
0164: */
0165: public StepDefinition getStepDefinition(long id) {
0166: for (StepDefinition sdef : stepDefinitions)
0167: if (sdef.getId() == id)
0168: return sdef;
0169: throw new FxNotFoundException(
0170: "ex.stepdefinition.load.notFound", id)
0171: .asRuntimeException();
0172: }
0173:
0174: /**
0175: * {@inheritDoc}
0176: */
0177: public Step getStepByDefinition(long workflowId,
0178: long stepDefinitionId) {
0179: // Find the step
0180: for (Step step : steps) {
0181: if (step.getWorkflowId() != workflowId)
0182: continue;
0183: if (step.getStepDefinitionId() != stepDefinitionId)
0184: continue;
0185: return step;
0186: }
0187:
0188: // Step does not exist
0189: throw new FxNotFoundException(
0190: "ex.stepdefinition.notFound.id.workflow",
0191: stepDefinitionId, workflowId).asRuntimeException();
0192: }
0193:
0194: /**
0195: * {@inheritDoc}
0196: */
0197: public List<Step> getStepsByDefinition(long stepDefinitionId) {
0198: // Find the step
0199: ArrayList<Step> list = new ArrayList<Step>();
0200: for (Step step : steps)
0201: if (step.getStepDefinitionId() == stepDefinitionId)
0202: list.add(step);
0203: return Collections.unmodifiableList(list);
0204: }
0205:
0206: /**
0207: * {@inheritDoc}
0208: */
0209: public List<Step> getStepsByWorkflow(long workflowId) {
0210: ArrayList<Step> list = new ArrayList<Step>();
0211: for (Step step : steps)
0212: if (step.getWorkflowId() == workflowId)
0213: list.add(step);
0214: return list;
0215: }
0216:
0217: /**
0218: * {@inheritDoc}
0219: */
0220: public Step getStep(long stepId) {
0221: for (Step step : steps)
0222: if (step.getId() == stepId)
0223: return step;
0224: throw new FxNotFoundException("ex.step.notFound.id", stepId)
0225: .asRuntimeException();
0226: }
0227:
0228: /**
0229: * Assign all steps
0230: *
0231: * @param steps all steps
0232: */
0233: protected void setSteps(List<Step> steps) {
0234: this .steps = steps;
0235: }
0236:
0237: /**
0238: * {@inheritDoc}
0239: */
0240: public List<Step> getSteps() {
0241: return Collections.unmodifiableList(steps);
0242: }
0243:
0244: protected void setWorkflows(List<Workflow> workflows) {
0245: this .workflows = workflows;
0246: }
0247:
0248: /**
0249: * Assign all defined mandators
0250: *
0251: * @param mandators all defined mandators
0252: */
0253: public void setMandators(Mandator[] mandators) {
0254: this .mandators = FxArrayUtils.clone(mandators);
0255: }
0256:
0257: /**
0258: * Assign all defined select lists
0259: *
0260: * @param lists select lists
0261: */
0262: public void setSelectLists(List<FxSelectList> lists) {
0263: this .selectLists = lists;
0264: }
0265:
0266: /**
0267: * Assign all defined groups
0268: *
0269: * @param groups all defined groups
0270: */
0271: protected void setGroups(List<FxGroup> groups) {
0272: this .groups = groups;
0273: }
0274:
0275: /**
0276: * Assign all defined properties
0277: *
0278: * @param properties all defined properties
0279: */
0280: protected void setProperties(List<FxProperty> properties) {
0281: this .properties = properties;
0282: }
0283:
0284: /**
0285: * Assign all defined types
0286: *
0287: * @param fxTypes all defined types
0288: */
0289: protected void setTypes(List<FxType> fxTypes) {
0290: this .types = fxTypes;
0291: }
0292:
0293: /**
0294: * Assign FxAssignments (mixed groups/properties)
0295: *
0296: * @param assignments all assignments (mixed groups/properties)
0297: */
0298: protected void setAssignments(List<FxAssignment> assignments) {
0299: this .assignments = assignments;
0300: if (propertyAssignmentsAll != null)
0301: propertyAssignmentsAll.clear();
0302: else
0303: propertyAssignmentsAll = new ArrayList<FxPropertyAssignment>(
0304: assignments.size() / 2);
0305: if (propertyAssignmentsEnabled != null)
0306: propertyAssignmentsEnabled.clear();
0307: else
0308: propertyAssignmentsEnabled = new ArrayList<FxPropertyAssignment>(
0309: assignments.size() / 2);
0310: if (propertyAssignmentsSystemInternalRoot != null)
0311: propertyAssignmentsSystemInternalRoot.clear();
0312: else
0313: propertyAssignmentsSystemInternalRoot = new ArrayList<FxPropertyAssignment>(
0314: 25);
0315: if (groupAssignmentsAll != null)
0316: groupAssignmentsAll.clear();
0317: else
0318: groupAssignmentsAll = new ArrayList<FxGroupAssignment>(
0319: assignments.size() / 2);
0320: if (groupAssignmentsEnabled != null)
0321: groupAssignmentsEnabled.clear();
0322: else
0323: groupAssignmentsEnabled = new ArrayList<FxGroupAssignment>(
0324: assignments.size() / 2);
0325:
0326: for (FxAssignment curr : assignments) {
0327: if (curr instanceof FxPropertyAssignment) {
0328: propertyAssignmentsAll.add((FxPropertyAssignment) curr);
0329: if (curr.isEnabled())
0330: propertyAssignmentsEnabled
0331: .add((FxPropertyAssignment) curr);
0332: if (((FxPropertyAssignment) curr).getProperty()
0333: .isSystemInternal()
0334: && curr.getAssignedType().getId() == 0)
0335: propertyAssignmentsSystemInternalRoot
0336: .add((FxPropertyAssignment) curr);
0337: } else if (curr instanceof FxGroupAssignment) {
0338: groupAssignmentsAll.add((FxGroupAssignment) curr);
0339: if (curr.isEnabled())
0340: groupAssignmentsEnabled
0341: .add((FxGroupAssignment) curr);
0342: } else {
0343: // LOG.error("Unknown assignment class: " + curr.getClass());
0344: //TODO: throw exception
0345: }
0346: }
0347: Collections.sort(propertyAssignmentsSystemInternalRoot);
0348: }
0349:
0350: /**
0351: * Set scripts
0352: *
0353: * @param scripts all scripts
0354: */
0355: public void setScripts(List<FxScriptInfo> scripts) {
0356: this .scripts = scripts;
0357: }
0358:
0359: /**
0360: * Set script mappings
0361: *
0362: * @param scriptMappings all mappings
0363: */
0364: public void setScriptMappings(List<FxScriptMapping> scriptMappings) {
0365: this .scriptMappings = scriptMappings;
0366: }
0367:
0368: /**
0369: * {@inheritDoc}
0370: */
0371: public List<FxDataType> getDataTypes() {
0372: return Collections.unmodifiableList(dataTypes);
0373: }
0374:
0375: /**
0376: * {@inheritDoc}
0377: */
0378: public FxDataType getDataType(long id) {
0379: for (FxDataType dataType : dataTypes)
0380: if (dataType.getId() == id)
0381: return dataType;
0382: throw new FxNotFoundException(
0383: "ex.structure.dataType.notFound.id", id)
0384: .asRuntimeException();
0385: }
0386:
0387: /**
0388: * {@inheritDoc}
0389: */
0390: public ACL getACL(long id) {
0391: for (ACL acl : acls)
0392: if (acl.getId() == id)
0393: return acl;
0394: throw new FxNotFoundException("ex.structure.acl.notFound.id",
0395: id).asRuntimeException();
0396: }
0397:
0398: /**
0399: * {@inheritDoc}
0400: */
0401: public ACL getACL(String name) {
0402: for (ACL acl : acls)
0403: if (acl.getName().equals(name))
0404: return acl;
0405: throw new FxNotFoundException("ex.structure.acl.notFound.name",
0406: name).asRuntimeException();
0407: }
0408:
0409: /**
0410: * {@inheritDoc}
0411: */
0412: public List<ACL> getACLs() {
0413: return Collections.unmodifiableList(acls);
0414: }
0415:
0416: /**
0417: * {@inheritDoc}
0418: */
0419: public List<ACL> getACLs(ACL.Category category) {
0420: List<ACL> result = new ArrayList<ACL>(acls.size());
0421: for (ACL acl : acls) {
0422: if (acl.getCategory() == category) {
0423: result.add(acl);
0424: }
0425: }
0426: return Collections.unmodifiableList(result);
0427: }
0428:
0429: /**
0430: * {@inheritDoc}
0431: */
0432: public List<ACL> getACLs(long mandatorId) {
0433: return getACLs(mandatorId, null, true);
0434: }
0435:
0436: /**
0437: * {@inheritDoc}
0438: */
0439: public List<ACL> getACLs(long mandatorId,
0440: boolean includeForeignAccessible) {
0441: return getACLs(mandatorId, null, includeForeignAccessible);
0442: }
0443:
0444: /**
0445: * {@inheritDoc}
0446: */
0447: public List<ACL> getACLs(long mandatorId, ACL.Category category,
0448: boolean includeForeignAccessible) {
0449: final UserTicket ticket = FxContext.get().getTicket();
0450: final List<ACL> result = new ArrayList<ACL>();
0451: for (ACL acl : acls) {
0452: if ((acl.getMandatorId() == mandatorId // mandator filter matches
0453: || (includeForeignAccessible && ticket
0454: .isAssignedToACL(acl.getId()))) // user assigned to mandator-foreign ACL
0455: && (category == null || category.equals(acl
0456: .getCategory()))) { // category filter matches
0457: result.add(acl);
0458: }
0459: }
0460: return Collections.unmodifiableList(result);
0461: }
0462:
0463: /**
0464: * {@inheritDoc}
0465: */
0466: public ACL getDefaultACL(ACL.Category category) {
0467: return getACL(category.getDefaultId());
0468: }
0469:
0470: /**
0471: * {@inheritDoc}
0472: */
0473: public Workflow getWorkflow(long id) {
0474: for (Workflow wf : workflows)
0475: if (wf.getId() == id)
0476: return wf;
0477: throw new FxNotFoundException(
0478: "ex.structure.workflow.notFound.id", id)
0479: .asRuntimeException();
0480: }
0481:
0482: /**
0483: * {@inheritDoc}
0484: */
0485: public List<Workflow> getWorkflows() {
0486: return Collections.unmodifiableList(workflows);
0487: }
0488:
0489: /**
0490: * {@inheritDoc}
0491: */
0492: public Mandator getMandator(long id) {
0493: for (Mandator mandator : mandators)
0494: if (mandator.getId() == id)
0495: return mandator;
0496: throw new FxNotFoundException(
0497: "ex.structure.mandator.notFound.id", id)
0498: .asRuntimeException();
0499: }
0500:
0501: /**
0502: * {@inheritDoc}
0503: */
0504: public Mandator getMandator(String name) {
0505: for (Mandator mandator : mandators) {
0506: if (mandator.getName().equals(name)) {
0507: return mandator;
0508: }
0509: }
0510: throw new FxNotFoundException(
0511: "ex.structure.mandator.notFound.name", name)
0512: .asRuntimeException();
0513: }
0514:
0515: /**
0516: * {@inheritDoc}
0517: */
0518: public List<Mandator> getMandators(boolean active, boolean inactive) {
0519: ArrayList<Mandator> mand = new ArrayList<Mandator>(10);
0520: for (Mandator mandator : mandators) {
0521: switch ((mandator.isActive() ? 1 : 0)) {
0522: case 1:
0523: if (active)
0524: mand.add(mandator);
0525: break;
0526: case 0:
0527: if (inactive)
0528: mand.add(mandator);
0529: break;
0530:
0531: }
0532: }
0533: return Collections.unmodifiableList(mand);
0534: }
0535:
0536: /**
0537: * {@inheritDoc}
0538: */
0539: public List<FxGroup> getGroups(boolean returnReferenced,
0540: boolean returnUnreferenced, boolean returnRootGroups,
0541: boolean returnSubGroups) {
0542: if (returnReferenced && returnUnreferenced && returnRootGroups
0543: && returnSubGroups) {
0544: return Collections.unmodifiableList(groups);
0545: }
0546: ArrayList<FxGroup> result = new ArrayList<FxGroup>(groups
0547: .size());
0548: boolean add;
0549: boolean foundRoot, foundSub;
0550: for (FxGroup group : groups) {
0551: add = returnReferenced && group.isReferenced();
0552: if (returnUnreferenced && !group.isReferenced())
0553: add = true;
0554: if (returnReferenced && !returnRootGroups
0555: && !returnSubGroups)
0556: continue;
0557: if (add && group.isReferenced()
0558: && !(returnRootGroups && returnSubGroups)) {
0559: //filter either root or sub groups
0560: foundRoot = foundSub = false;
0561: for (FxGroupAssignment ga : groupAssignmentsAll) {
0562: if (ga.getGroup().getId() == group.getId()) {
0563: if (ga.getParentGroupAssignment() == null)
0564: foundRoot = true;
0565: else
0566: foundSub = true;
0567: }
0568: }
0569: if (returnRootGroups && !foundRoot)
0570: add = false;
0571: if (returnSubGroups && !foundSub)
0572: add = false;
0573: }
0574: if (add)
0575: result.add(group);
0576: }
0577: return Collections.unmodifiableList(result);
0578: }
0579:
0580: /**
0581: * {@inheritDoc}
0582: */
0583: public FxGroup getGroup(long id) {
0584: for (FxGroup group : groups)
0585: if (group.getId() == id)
0586: return group;
0587: throw new FxNotFoundException("ex.structure.group.notFound.id",
0588: id).asRuntimeException();
0589: }
0590:
0591: /**
0592: * {@inheritDoc}
0593: */
0594: public FxGroup getGroup(String name) {
0595: for (FxGroup group : groups)
0596: if (group.getName().equalsIgnoreCase(name))
0597: return group;
0598: throw new FxNotFoundException(
0599: "ex.structure.group.notFound.name", name)
0600: .asRuntimeException();
0601: }
0602:
0603: /**
0604: * {@inheritDoc}
0605: */
0606: public List<FxProperty> getProperties(boolean returnReferenced,
0607: boolean returnUnreferenced) {
0608: if (returnReferenced && returnUnreferenced)
0609: return Collections.unmodifiableList(properties);
0610: ArrayList<FxProperty> result = new ArrayList<FxProperty>(
0611: properties.size());
0612: for (FxProperty prop : properties) {
0613: if (returnReferenced && prop.isReferenced())
0614: result.add(prop);
0615: if (returnUnreferenced && !prop.isReferenced())
0616: result.add(prop);
0617: }
0618: return Collections.unmodifiableList(result);
0619: }
0620:
0621: /**
0622: * {@inheritDoc}
0623: */
0624: public FxProperty getProperty(long id) {
0625: for (FxProperty property : properties)
0626: if (property.getId() == id)
0627: return property;
0628: throw new FxNotFoundException(
0629: "ex.structure.property.notFound.id", id)
0630: .asRuntimeException();
0631: }
0632:
0633: /**
0634: * {@inheritDoc}
0635: */
0636: public FxProperty getProperty(String name) {
0637: for (FxProperty property : properties)
0638: if (property.getName().equalsIgnoreCase(name))
0639: return property;
0640: throw new FxNotFoundException(
0641: "ex.structure.property.notFound.name", name)
0642: .asRuntimeException();
0643: }
0644:
0645: /**
0646: * {@inheritDoc}
0647: */
0648: public List<FxPropertyAssignment> getPropertyAssignments() {
0649: return getPropertyAssignments(false);
0650: }
0651:
0652: /**
0653: * {@inheritDoc}
0654: */
0655: public List<FxPropertyAssignment> getSystemInternalRootPropertyAssignments() {
0656: /*if (this.propertyAssignmentsSystemInternalRoot == null) {
0657: System.out.println("Null assignments!");
0658: new Throwable().printStackTrace();
0659: }*/
0660: return Collections
0661: .unmodifiableList(propertyAssignmentsSystemInternalRoot);
0662: }
0663:
0664: /**
0665: * {@inheritDoc}
0666: */
0667: public List<FxPropertyAssignment> getPropertyAssignments(
0668: boolean includeDisabled) {
0669: return Collections
0670: .unmodifiableList(includeDisabled ? propertyAssignmentsAll
0671: : propertyAssignmentsEnabled);
0672: }
0673:
0674: /**
0675: * {@inheritDoc}
0676: */
0677: public List<FxGroupAssignment> getGroupAssignments() {
0678: return getGroupAssignments(false);
0679: }
0680:
0681: /**
0682: * {@inheritDoc}
0683: */
0684: public List<FxGroupAssignment> getGroupAssignments(
0685: boolean includeDisabled) {
0686: return Collections
0687: .unmodifiableList(includeDisabled ? groupAssignmentsAll
0688: : groupAssignmentsEnabled);
0689: }
0690:
0691: /**
0692: * {@inheritDoc}
0693: */
0694: public List<FxType> getTypes(boolean returnBaseTypes,
0695: boolean returnDerivedTypes, boolean returnTypes,
0696: boolean returnRelations) {
0697: return Collections.unmodifiableList(_getTypes(returnBaseTypes,
0698: returnDerivedTypes, returnTypes, returnRelations));
0699: }
0700:
0701: /**
0702: * {@inheritDoc}
0703: */
0704: public List<FxType> getReferencingRelationTypes(long typeId) {
0705: ArrayList<FxType> relTypes = new ArrayList<FxType>();
0706: List<FxType> relations = getTypes(true, true, false, true);
0707: for (FxType t : relations) {
0708: for (FxTypeRelation r : t.getRelations()) {
0709: if (r.getDestination().getId() == typeId
0710: || r.getSource().getId() == typeId) {
0711: relTypes.add(t);
0712: break;
0713: }
0714: }
0715: }
0716: return Collections.unmodifiableList(relTypes);
0717: }
0718:
0719: /**
0720: * Get types depending on selection criteria
0721: *
0722: * @param returnBaseTypes return types that are not derived from another type
0723: * @param returnDerivedTypes return types that are derived from another type
0724: * @param returnTypes return FxTypes
0725: * @param returnRelations return FxTypes that are relations
0726: * @return FxType iterator
0727: */
0728: private List<FxType> _getTypes(boolean returnBaseTypes,
0729: boolean returnDerivedTypes, boolean returnTypes,
0730: boolean returnRelations) {
0731: if (returnBaseTypes && returnDerivedTypes && returnTypes
0732: && returnRelations)
0733: return this .types;
0734: ArrayList<FxType> ret = new ArrayList<FxType>(this .types.size());
0735: for (FxType t : types) {
0736: if (t.getMode() == TypeMode.Relation && returnRelations) {
0737: if (t.getParent() == null && returnBaseTypes)
0738: ret.add(t);
0739: else if (t.getParent() != null && returnDerivedTypes)
0740: ret.add(t);
0741: } else if (t.getMode() != TypeMode.Relation && returnTypes) {
0742: if (t.getParent() == null && returnBaseTypes)
0743: ret.add(t);
0744: else if (t.getParent() != null && returnDerivedTypes)
0745: ret.add(t);
0746: }
0747: }
0748: return ret;
0749: }
0750:
0751: /**
0752: * {@inheritDoc}
0753: */
0754: public FxAssignment getAssignment(String xPath) {
0755: if (xPath != null && xPath.trim().length() > 0) {
0756: try {
0757: xPath = XPathElement.toXPathNoMult(xPath);
0758: } catch (FxInvalidParameterException e) {
0759: throw e.asRuntimeException();
0760: }
0761: for (FxAssignment as : assignments)
0762: if (xPath.equals(as.getXPath()))
0763: return as;
0764: }
0765: throw new FxNotFoundException(
0766: "ex.structure.assignment.notFound.xpath", xPath)
0767: .asRuntimeException();
0768: }
0769:
0770: /**
0771: * {@inheritDoc}
0772: */
0773: public FxAssignment getAssignment(long assignmentId) {
0774: for (FxAssignment as : assignments)
0775: if (as.getId() == assignmentId)
0776: return as;
0777: throw new FxNotFoundException(
0778: "ex.structure.assignment.notFound.id", assignmentId)
0779: .asRuntimeException();
0780: }
0781:
0782: /**
0783: * {@inheritDoc}
0784: */
0785: public List<FxAssignment> getDerivedAssignments(long assignmentId) {
0786: List<FxAssignment> ret = null;
0787: for (FxAssignment as : assignments)
0788: if (as.getBaseAssignmentId() == assignmentId) {
0789: if (ret == null)
0790: ret = new ArrayList<FxAssignment>(5);
0791: ret.add(as);
0792: }
0793: if (ret == null)
0794: ret = new ArrayList<FxAssignment>(0);
0795: else {
0796: List<FxAssignment> ret2 = new ArrayList<FxAssignment>(0);
0797: for (FxAssignment as : ret) {
0798: ret2.addAll(getDerivedAssignments(as.getId()));
0799: }
0800: ret.addAll(ret2);
0801: }
0802: return ret;
0803: }
0804:
0805: /**
0806: * {@inheritDoc}
0807: */
0808: public FxType getType(String name) {
0809: for (FxType type : types)
0810: if (type.getName().equalsIgnoreCase(name))
0811: return type;
0812: throw new FxNotFoundException(
0813: "ex.structure.type.notFound.name", name)
0814: .asRuntimeException();
0815: }
0816:
0817: /**
0818: * {@inheritDoc}
0819: */
0820: public FxType getType(long id) {
0821: for (FxType type : types)
0822: if (type.getId() == id)
0823: return type;
0824: throw new FxNotFoundException("ex.structure.type.notFound.id",
0825: id).asRuntimeException();
0826: }
0827:
0828: /**
0829: * {@inheritDoc}
0830: */
0831: public List<FxType> getTypesForProperty(long propertyId) {
0832: List<FxType> ret = new ArrayList<FxType>(10);
0833: for (FxPropertyAssignment as : propertyAssignmentsAll) {
0834: if (as.getProperty().getId() != propertyId)
0835: continue;
0836: if (ret.contains(as.getAssignedType()))
0837: continue;
0838: ret.add(as.getAssignedType());
0839: }
0840: return Collections.unmodifiableList(ret);
0841: }
0842:
0843: /**
0844: * {@inheritDoc}
0845: */
0846: public Route getRoute(long routeId) {
0847: for (Workflow workflow : workflows) {
0848: for (Route route : workflow.getRoutes()) {
0849: if (route.getId() == routeId) {
0850: return route;
0851: }
0852: }
0853: }
0854: throw new FxNotFoundException("ex.structure.route.notFound.id",
0855: routeId).asRuntimeException();
0856: }
0857:
0858: /**
0859: * {@inheritDoc}
0860: */
0861: public List<FxScriptInfo> getScripts() {
0862: return scripts;
0863: }
0864:
0865: /**
0866: * {@inheritDoc}
0867: */
0868: public FxScriptInfo getScript(long scriptId) {
0869: for (FxScriptInfo si : this .scripts)
0870: if (si.getId() == scriptId)
0871: return si;
0872: throw new FxNotFoundException("ex.scripting.notFound", scriptId)
0873: .asRuntimeException();
0874: }
0875:
0876: /**
0877: * {@inheritDoc}
0878: */
0879: public List<FxScriptMapping> getScriptMappings() {
0880: return scriptMappings;
0881: }
0882:
0883: /**
0884: * {@inheritDoc}
0885: */
0886: public FxScriptMapping getScriptMapping(long scriptId) {
0887: for (FxScriptMapping mapping : this .scriptMappings)
0888: if (mapping.getScriptId() == scriptId)
0889: return mapping;
0890: throw new FxNotFoundException("ex.scripting.notFound", scriptId)
0891: .asRuntimeException();
0892: }
0893:
0894: /**
0895: * Resolve all missing dependencies
0896: *
0897: * @throws FxNotFoundException if a dependency could not be resolved
0898: */
0899: protected void resolveDependencies() throws FxNotFoundException {
0900: for (FxType type : types)
0901: type.resolveReferences(this );
0902: //calculate if properties and groups are referenced
0903: boolean ref;
0904: if (properties != null)
0905: for (FxProperty prop : properties) {
0906: ref = false;
0907: for (FxAssignment as : this .propertyAssignmentsAll)
0908: if (as instanceof FxPropertyAssignment
0909: && ((FxPropertyAssignment) as)
0910: .getProperty().getId() == prop
0911: .getId()) {
0912: ref = true;
0913: }
0914: prop.setReferenced(ref);
0915: if (prop.getReferencedType() != null) {
0916: prop.resolveReferencedType(this );
0917: }
0918: }
0919: if (groups != null)
0920: for (FxGroup group : groups) {
0921: ref = false;
0922: for (FxAssignment as : this .groupAssignmentsAll)
0923: if (as instanceof FxGroupAssignment
0924: && ((FxGroupAssignment) as).getGroup()
0925: .getId() == group.getId()) {
0926: ref = true;
0927: }
0928: group.setReferenced(ref);
0929: }
0930: for (FxAssignment as : this .propertyAssignmentsAll)
0931: as.resolveReferences(this );
0932: for (FxAssignment as : this .groupAssignmentsAll)
0933: as.resolveReferences(this );
0934: for (FxSelectList list : this .getSelectLists())
0935: list._synchronize(this );
0936: //2nd pass for types (scripting for assignments can only be resolved now)
0937: for (FxType type : types)
0938: type.resolveReferences(this );
0939: }
0940:
0941: /**
0942: * Update or add an existing ACL
0943: *
0944: * @param _acl ACL to update/add
0945: */
0946: protected void updateACL(ACL _acl) {
0947: for (int i = 0; i < acls.size(); i++)
0948: if (acls.get(i).getId() == _acl.getId()) {
0949: acls.remove(i);
0950: acls.add(_acl);
0951: return;
0952: }
0953: acls.add(_acl); //add new one
0954: }
0955:
0956: /**
0957: * Remove an existing ACL
0958: *
0959: * @param id ACL to remove
0960: */
0961: protected void removeACL(long id) {
0962: for (int i = 0; i < acls.size(); i++)
0963: if (acls.get(i).getId() == id) {
0964: acls.remove(i);
0965: return;
0966: }
0967: }
0968:
0969: /**
0970: * Update or add a FxType
0971: *
0972: * @param type type to update/add
0973: * @throws FxNotFoundException on dependency errors
0974: */
0975: public void updateType(FxType type) throws FxNotFoundException {
0976: try {
0977: FxType org = getType(type.getId());
0978: types.set(types.indexOf(org), type);
0979: } catch (FxRuntimeException e) {
0980: //new type
0981: types.add(type);
0982: }
0983: resolveDependencies();
0984: }
0985:
0986: /**
0987: * Add a mandator
0988: *
0989: * @param mandator mandator
0990: */
0991: protected void addMandator(Mandator mandator) {
0992: mandators = FxArrayUtils.addElement(mandators, mandator, true);
0993: }
0994:
0995: /**
0996: * Update a mandator, silently fails if the mandator does not exist
0997: *
0998: * @param mandator mandator
0999: */
1000: public void updateMandator(Mandator mandator) {
1001: for (int i = 0; i < mandators.length; i++) {
1002: if (mandators[i].getId() == mandator.getId()) {
1003: mandators[i] = mandator;
1004: return;
1005: }
1006: }
1007: inactiveMandators = null;
1008: }
1009:
1010: /**
1011: * Remove a mandator
1012: *
1013: * @param mandatorId mandator id to remove
1014: */
1015: public void removeMandator(long mandatorId) {
1016: ArrayList<Mandator> al = new ArrayList<Mandator>(
1017: mandators.length - 1);
1018: for (Mandator mandator : mandators) {
1019: if (mandator.getId() != mandatorId)
1020: al.add(mandator);
1021: }
1022: mandators = al.toArray(new Mandator[al.size()]);
1023: }
1024:
1025: /**
1026: * Update scripts after changes
1027: *
1028: * @param scripts all scripts
1029: * @param scriptMapping all mappings
1030: * @throws FxNotFoundException if dependencies can not be resolved
1031: */
1032: public void updateScripting(List<FxScriptInfo> scripts,
1033: List<FxScriptMapping> scriptMapping)
1034: throws FxNotFoundException {
1035: this .scripts = scripts;
1036: this .scriptMappings = scriptMapping;
1037: resolveDependencies();
1038: }
1039:
1040: /**
1041: * Perform a 'deep' clone (copy) of this instance
1042: *
1043: * @return FxEnvironmentImpl
1044: */
1045: public FxEnvironmentImpl deepClone() {
1046: return new FxEnvironmentImpl(this );
1047: }
1048:
1049: /**
1050: * Update the timestamp of the environment to the current time
1051: */
1052: public void updateTimeStamp() {
1053: this .timeStamp = System.currentTimeMillis();
1054: }
1055:
1056: /**
1057: * {@inheritDoc}
1058: */
1059: public long getTimeStamp() {
1060: return this .timeStamp;
1061: }
1062:
1063: /**
1064: * {@inheritDoc}
1065: */
1066: public List<FxSelectList> getSelectLists() {
1067: return Collections.unmodifiableList(this .selectLists);
1068: }
1069:
1070: /**
1071: * {@inheritDoc}
1072: */
1073: public FxSelectList getSelectList(long id) {
1074: for (FxSelectList list : this .selectLists)
1075: if (id == list.getId())
1076: return list;
1077: throw new FxNotFoundException("ex.structure.list.notFound", id)
1078: .asRuntimeException();
1079: }
1080:
1081: /**
1082: * {@inheritDoc}
1083: */
1084: public FxSelectList getSelectList(String name) {
1085: for (FxSelectList list : this .selectLists)
1086: if (list.getName().equals(name))
1087: return list;
1088: throw new FxNotFoundException("ex.structure.list.notFound",
1089: name).asRuntimeException();
1090: }
1091:
1092: /**
1093: * {@inheritDoc}
1094: */
1095: public FxSelectListItem getSelectListItem(long id) {
1096: for (FxSelectList list : this .selectLists)
1097: if (list.containsItem(id))
1098: return list.getItem(id);
1099: throw new FxNotFoundException(
1100: "ex.structure.list.item.notFound", id)
1101: .asRuntimeException();
1102: }
1103:
1104: /**
1105: * {@inheritDoc}
1106: */
1107: public String getInactiveMandatorList() {
1108: if (inactiveMandators != null)
1109: return inactiveMandators;
1110: StringBuilder sb = new StringBuilder(50);
1111: for (Mandator m : mandators) {
1112: if (!m.isActive()) {
1113: if (sb.length() > 0)
1114: sb.append(',');
1115: sb.append(m.getId());
1116: }
1117: }
1118: inactiveMandators = sb.toString();
1119: return inactiveMandators;
1120: }
1121:
1122: /**
1123: * {@inheritDoc}
1124: */
1125: public String getDeactivatedTypesList() {
1126: if (deactivatedTypes != null)
1127: return deactivatedTypes;
1128: StringBuilder sb = new StringBuilder(50);
1129: for (FxType t : types)
1130: if (t.getState() == TypeState.Unavailable) {
1131: if (sb.length() > 0)
1132: sb.append(',');
1133: sb.append(t.getId());
1134: }
1135: deactivatedTypes = sb.toString();
1136: return deactivatedTypes;
1137: }
1138: }
|