示例#1
0
 @Override
 public void setUp() throws Exception {
   facet = new HandsFacet();
   super.setUp();
   DataSetID cid = DataSetID.getID();
   id = CharID.getID(cid);
   altid = CharID.getID(cid);
   facet.setRaceFacet(rfacet);
   facet.setTemplateFacet(tfacet);
 }
示例#2
0
 @Override
 public void setUp() throws Exception {
   super.setUp();
   DataSetID cid = DataSetID.getID();
   id = CharID.getID(cid);
   altid = CharID.getID(cid);
   facet = getMockFacet();
   facet.setRaceFacet(rfacet);
   facet.setTemplateFacet(tfacet);
   facet.setFormulaResolvingFacet(new FormulaResolvingFacet());
   bonusInfo = new HashMap<CharID, Double>();
   staticSetUp();
 }
示例#3
0
public abstract class LoadContextInst implements LoadContext {

  private static final PrerequisiteWriter PREREQ_WRITER = new PrerequisiteWriter();

  private final DataSetID datasetID = DataSetID.getID();

  private final AbstractListContext list;

  private final AbstractObjectContext obj;

  private final AbstractReferenceContext ref;

  private final List<Campaign> campaignList = new ArrayList<Campaign>();

  private int writeMessageCount = 0;

  private final TokenSupport support = new TokenSupport();

  private final List<Object> dontForget = new ArrayList<Object>();

  // Per file
  private URI sourceURI;

  // Per file
  private CDOMObject stateful;

  static {
    FacetInitialization.initialize();
  }

  public LoadContextInst(
      AbstractReferenceContext rc, AbstractListContext lc, AbstractObjectContext oc) {
    if (rc == null) {
      throw new IllegalArgumentException("ReferenceContext cannot be null");
    }
    if (lc == null) {
      throw new IllegalArgumentException("ListContext cannot be null");
    }
    if (oc == null) {
      throw new IllegalArgumentException("ObjectContext cannot be null");
    }
    ref = rc;
    list = lc;
    obj = oc;
  }

  public void addWriteMessage(String string) {
    Logging.errorPrint("!!" + string);
    /*
     * TODO Need to find a better solution for what happens during write...
     */
    writeMessageCount++;
  }

  public int getWriteMessageCount() {
    return writeMessageCount;
  }

  /**
   * Sets the extract URI. This is a shortcut for setting the URI on both the graph and obj members.
   *
   * @param extractURI
   */
  public void setExtractURI(URI extractURI) {
    getObjectContext().setExtractURI(extractURI);
    getReferenceContext().setExtractURI(extractURI);
    getListContext().setExtractURI(extractURI);
  }

  /**
   * Sets the source URI. This is a shortcut for setting the URI on both the graph and obj members.
   *
   * @param sourceURI
   */
  public void setSourceURI(URI sourceURI) {
    this.sourceURI = sourceURI;
    getObjectContext().setSourceURI(sourceURI);
    getReferenceContext().setSourceURI(sourceURI);
    getListContext().setSourceURI(sourceURI);
    clearStatefulInformation();
    Logging.debugPrint("Starting Load of " + sourceURI);
  }

  public URI getSourceURI() {
    return sourceURI;
  }

  /*
   * Get the type of context we're running in (either Editor or Runtime)
   */
  public abstract String getContextType();

  public AbstractObjectContext getObjectContext() {
    return obj;
  }

  public AbstractListContext getListContext() {
    return list;
  }

  public void commit() {
    getListContext().commit();
    getObjectContext().commit();
  }

  public void rollback() {
    getListContext().rollback();
    getObjectContext().rollback();
  }

  public void resolveDeferredTokens() {
    for (DeferredToken<? extends Loadable> token : support.getDeferredTokens()) {
      processRes(token);
    }
    commit();
  }

  private <T extends Loadable> void processRes(DeferredToken<T> token) {
    Class<T> cl = token.getDeferredTokenClass();
    Collection<? extends ReferenceManufacturer> mfgs = getReferenceContext().getAllManufacturers();
    for (ReferenceManufacturer<? extends T> rm : mfgs) {
      if (cl.isAssignableFrom(rm.getReferenceClass())) {
        for (T po : rm.getAllObjects()) {
          token.process(this, po);
        }
        for (T po : rm.getDerivativeObjects()) {
          token.process(this, po);
        }
      }
    }
  }

  public void resolvePostDeferredTokens() {
    Collection<? extends ReferenceManufacturer> mfgs = getReferenceContext().getAllManufacturers();
    for (PostDeferredToken<? extends Loadable> token : TokenLibrary.getPostDeferredTokens()) {
      processPostRes(token, mfgs);
    }
  }

  private <T extends Loadable> void processPostRes(
      PostDeferredToken<T> token, Collection<? extends ReferenceManufacturer> mfgs) {
    Class<T> cl = token.getDeferredTokenClass();
    for (ReferenceManufacturer<? extends T> rm : mfgs) {
      if (cl.isAssignableFrom(rm.getReferenceClass())) {
        for (T po : rm.getAllObjects()) {
          this.setSourceURI(po.getSourceURI());
          token.process(this, po);
        }
      }
    }
  }

  public <T extends CDOMObject> PrimitiveCollection<T> getChoiceSet(
      SelectionCreator<T> sc, String value) {
    try {
      return ChoiceSetLoadUtilities.getChoiceSet(this, sc, value);
    } catch (ParsingSeparator.GroupingMismatchException e) {
      Logging.errorPrint("Group Mismatch in getting ChoiceSet: " + e.getMessage());
      return null;
    }
  }

  public <T extends CDOMObject> PrimitiveCollection<T> getPrimitiveChoiceFilter(
      SelectionCreator<T> sc, String key) {
    return ChoiceSetLoadUtilities.getPrimitive(this, sc, key);
  }

  public <T> ParseResult processSubToken(T cdo, String tokenName, String key, String value) {
    return support.processSubToken(this, cdo, tokenName, key, value);
  }

  public <T extends Loadable> boolean processToken(T derivative, String typeStr, String argument)
      throws PersistenceLayerException {
    return support.processToken(this, derivative, typeStr, argument);
  }

  public <T extends Loadable> void unconditionallyProcess(T cdo, String key, String value) {
    try {
      if (processToken(cdo, key, value)) {
        commit();
      } else {
        rollback();
        Logging.replayParsedMessages();
      }
      Logging.clearParseMessages();
    } catch (PersistenceLayerException e) {
      Logging.errorPrint("Error in token parse: " + e.getLocalizedMessage());
    }
  }

  /**
   * Produce the LST code for any occurrences of subtokens of the parent token.
   *
   * @param <T> The type of object to be processed, generally a CDOMObject.
   * @param cdo The object to be partially unparsed
   * @param tokenName The name of the parent token
   * @return An array of LST code 'fields' all of which are subtokens of the parent token.
   */
  public <T> String[] unparseSubtoken(T cdo, String tokenName) {
    return support.unparseSubtoken(this, cdo, tokenName);
  }

  public <T> Collection<String> unparse(T cdo) {
    return support.unparse(this, cdo);
  }

  public <T extends CDOMObject> T cloneConstructedCDOMObject(T cdo, String newName) {
    T newObj = getObjectContext().cloneConstructedCDOMObject(cdo, newName);
    getReferenceContext().importObject(newObj);
    return newObj;
  }

  /**
   * Create a copy of a CDOMObject duplicating any references to the old object. (e.g. Spell, Domain
   * etc)
   *
   * <p>Package protected rather than private for testing only
   *
   * @param cdo The original object being copied.
   * @param newName The name that should be given to the new object.
   * @return The newly created CDOMObject.
   */
  @SuppressWarnings("unchecked")
  <T extends CDOMObject> T cloneInMasterLists(T cdo, String newName) {
    T newObj;
    try {
      newObj = (T) cdo.clone();
      newObj.setName(newName);
      getListContext().cloneInMasterLists(cdo, newObj);
    } catch (CloneNotSupportedException e) {
      Logging.errorPrint("Failed to clone " + cdo, e);
      return null;
    }
    return newObj;
  }

  public String getPrerequisiteString(Collection<Prerequisite> prereqs) {
    try {
      return PREREQ_WRITER.getPrerequisiteString(prereqs);
    } catch (PersistenceLayerException e) {
      addWriteMessage("Error writing Prerequisite: " + e);
      return null;
    }
  }

  public CampaignSourceEntry getCampaignSourceEntry(Campaign source, String value) {
    return CampaignSourceEntry.getNewCSE(source, sourceURI, value);
  }

  public void clearStatefulInformation() {
    stateful = null;
  }

  public boolean addStatefulToken(String s) throws PersistenceLayerException {
    int colonLoc = s.indexOf(':');
    if (colonLoc == -1) {
      Logging.errorPrint("Found invalid stateful token: " + s);
      return false;
    }
    if (stateful == null) {
      stateful = new ObjectCache();
    }
    return processToken(
        stateful, s.substring(0, colonLoc).intern(), s.substring(colonLoc + 1).intern());
  }

  public void addStatefulInformation(CDOMObject target) {
    if (stateful != null) {
      target.overlayCDOMObject(stateful);
    }
  }

  public void setLoaded(List<Campaign> campaigns) {
    campaignList.clear();
    campaignList.addAll(campaigns);
  }

  public abstract boolean consolidate();

  public DataSetID getDataSetID() {
    return datasetID;
  }

  public void loadCampaignFacets() {
    FacetLibrary.getFacet(DataSetInitializationFacet.class).initialize(this);
  }

  public void forgetMeNot(CDOMReference<?> cdr) {
    dontForget.add(cdr);
  }

  public AbstractReferenceContext getReferenceContext() {
    return ref;
  }

  public List<Campaign> getLoadedCampaigns() {
    return Collections.unmodifiableList(campaignList);
  }

  public ReferenceManufacturer<? extends Loadable> getManufacturer(String firstToken) {
    return ReferenceContextUtilities.getManufacturer(getReferenceContext(), firstToken);
  }

  public <T extends CDOMObject> T performCopy(T object, String copyName) {
    T copy = ref.performCopy(object, copyName);
    list.cloneInMasterLists(object, copy);
    return copy;
  }
}