001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.function.v1;
009:
010: //base classes
011: import java.math.BigDecimal;
012:
013: //project specific classes
014: import org.jfolder.common.SelfArgumentlessConstructor;
015: import org.jfolder.common.StandardDataTypes;
016: import org.jfolder.common.UnexpectedSystemException;
017: import org.jfolder.common.function.BaseCommonFunctionCompatibilityTag;
018: import org.jfolder.common.function.WebPageTagInstanceViewContext;
019: import org.jfolder.common.function.WebPageTagInstanceViewContextInfo;
020: import org.jfolder.common.tagging.AppraiseConceptTagContext;
021: import org.jfolder.common.tagging.ConceptTag;
022: import org.jfolder.common.tagging.ConceptTagCharacteristic;
023: import org.jfolder.common.tagging.ConceptTagConstraint;
024: import org.jfolder.common.tagging.ConceptTagConstraintHolder;
025: import org.jfolder.common.tagging.ConceptTagFlagsHelper;
026: import org.jfolder.common.tagging.ConceptTagHelper;
027: import org.jfolder.common.tagging.DetermineClassConceptTagContext;
028: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagContext;
029: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagException;
030: import org.jfolder.common.tagging.DynamicConceptTagConstraintContext;
031: import org.jfolder.common.tagging.InitializeConceptTagContext;
032: import org.jfolder.common.tagging.ReturnableConceptTag;
033: import org.jfolder.common.tagging.SelectionCriteriaForReturnableConceptTag;
034: import org.jfolder.common.tagging.StaticConceptTagConstraintContext;
035: import org.jfolder.common.tagging.StudioConceptTagCreParInfoContext;
036: import org.jfolder.common.tagging.StudioConceptTagInstanceInfoContext;
037: import org.jfolder.common.tagging.StudioConceptTagTypeInfoContext;
038: import org.jfolder.common.tagging.ValueAndClassForConceptTag;
039: import org.jfolder.common.utils.misc.CommonExpressions;
040: import org.jfolder.common.utils.misc.MiscHelper;
041:
042: //other classes
043:
044: public class CommonFunctionMathMultiplyTag extends
045: BaseCommonFunctionCompatibilityTag implements
046: SelfArgumentlessConstructor {
047:
048: //characteristics
049: private ConceptTagCharacteristic factorCtchar = null;
050:
051: //factor details
052: private final static String FACTOR_CTCON = "factor";
053: private final static String FACTOR_TITLE = "Factors";
054: private final static String FACTOR_SHORT_DESCRIPTION = "Numbers to be multiplied to produce the result "
055: + "('Factor' is a fancy term meaning one of these numbers)";
056:
057: public void initialize(InitializeConceptTagContext inIctc) {
058:
059: //
060: ConceptTagConstraintHolder rootCtconh = ConceptTagConstraintHolder
061: .newInstance(inIctc);
062:
063: //constrain "factor"
064: ConceptTagConstraint factorCtcon = ConceptTagConstraint
065: .newInstance(FACTOR_CTCON, rootCtconh);
066: factorCtcon.setMinimumLength(2);
067: factorCtcon.setLengthUnbounded(true);
068: factorCtcon.setTitle(FACTOR_TITLE);
069: factorCtcon.setShortDescription(FACTOR_SHORT_DESCRIPTION);
070: //
071: StaticConceptTagConstraintContext factorSctcc = StaticConceptTagConstraintContext
072: .newInstance(factorCtcon);
073: factorSctcc.setRegularExpression(CommonExpressions.DECIMAL);
074: //
075: DynamicConceptTagConstraintContext factorDctcc = DynamicConceptTagConstraintContext
076: .newInstance(factorCtcon);
077: factorDctcc.getValidator().allowReturnOfOnlyDecimal();
078: factorDctcc.getValidator().filterDirectChildOfRootAndHtml();
079: //
080: factorSctcc.addDefaultValue("0");
081: factorSctcc.addDefaultValue("0");
082:
083: //
084: ConceptTagFlagsHelper
085: .pushNonIterativeCommonFunctionDepth(inIctc);
086: inIctc.initialize();
087: ConceptTagFlagsHelper
088: .popNonIterativeCommonFunctionDepth(inIctc);
089:
090: //initialize characteristics
091: this .factorCtchar = factorCtcon.getCharacteristic();
092:
093: }
094:
095: public void appraise(AppraiseConceptTagContext inActc) {
096:
097: if (inActc instanceof StudioConceptTagInstanceInfoContext) {
098:
099: StudioConceptTagInstanceInfoContext sctiic = (StudioConceptTagInstanceInfoContext) inActc;
100:
101: StringBuffer titleSb = new StringBuffer("");
102: StringBuffer descSb = new StringBuffer("");
103: for (int i = 0; i < this .factorCtchar.getValueLength(); i++) {
104: if (i > 0) {
105: titleSb.append(" * ");
106: descSb.append(" * ");
107: }
108: if (this .factorCtchar.isHolderWithConceptTag(i, inActc)) {
109: StudioConceptTagInstanceInfoContext nextStiic = this .factorCtchar
110: .getInstanceInformation(i);
111:
112: titleSb.append(nextStiic.getInstanceTitle());
113: descSb.append(nextStiic.getInstanceDescription());
114: } else if (this .factorCtchar.isStatic(i, inActc)) {
115: titleSb.append(this .factorCtchar.getValueAsString(
116: i, inActc));
117: descSb.append(this .factorCtchar.getValueAsString(i,
118: inActc));
119: }
120: }
121: //titleSb.append(")");
122: //descSb.append(")");
123: sctiic.setInstanceTitle(titleSb.toString());
124: sctiic.setInstanceDescription(descSb.toString());
125: } else if (inActc instanceof StudioConceptTagTypeInfoContext) {
126:
127: StudioConceptTagTypeInfoContext scttic = (StudioConceptTagTypeInfoContext) inActc;
128:
129: scttic.setTypeTitle("Multiply");
130: scttic.setTypeDescription("Multiplies two or more numbers");
131: } else if (inActc instanceof WebPageTagInstanceViewContext) {
132:
133: WebPageTagInstanceViewContext wptivc = (WebPageTagInstanceViewContext) inActc;
134:
135: WebPageTagInstanceViewContextInfo info = wptivc
136: .getInfoAsNonHtml();
137:
138: //
139: info.setNonHtmlContent("Multiply(", ")");
140: for (int i = 0; i < this .factorCtchar.getValueLength(); i++) {
141: //
142: WebPageTagInstanceViewContextInfo nextAddendInfo = info
143: .createNonHtmlInfo();
144: if (i > 0) {
145: nextAddendInfo.setNonHtmlContent(" * ");
146: }
147: WebPageTagInstanceViewContext.displayView(
148: this .factorCtchar, nextAddendInfo, wptivc, i,
149: i + 1);
150: }
151: //
152: if (this .factorCtchar.getValueLength() == 0) {
153: WebPageTagInstanceViewContextInfo subInfo = info
154: .createNonHtmlInfo(FACTOR_CTCON);
155: subInfo.setNonHtmlContent("???");
156: }
157: } else if (inActc instanceof SelectionCriteriaForReturnableConceptTag) {
158:
159: SelectionCriteriaForReturnableConceptTag scfrct = (SelectionCriteriaForReturnableConceptTag) inActc;
160:
161: scfrct.canReturnDecimal();
162: scfrct.declareFormatAsPlain();
163: } else if (inActc instanceof StudioConceptTagCreParInfoContext) {
164:
165: StudioConceptTagCreParInfoContext scpc = (StudioConceptTagCreParInfoContext) inActc;
166: }
167: }
168:
169: public void determineValueAndClass(
170: DetermineValueAndClassConceptTagContext inDvacctc) {
171:
172: try {
173: ValueAndClassForConceptTag vacfct = null;
174:
175: BigDecimal productBd = new BigDecimal(1);
176:
177: for (int i = 0; i < this .factorCtchar.getValueLength(); i++) {
178: if (this .factorCtchar.isStatic(i, inDvacctc)) {
179: BigDecimal bd = new BigDecimal(this .factorCtchar
180: .getValueAsString(i, inDvacctc));
181: productBd = productBd.multiply(bd);
182: } else {
183: ValueAndClassForConceptTag factorVac = this .factorCtchar
184: .getDetermineValueAndClass(i, inDvacctc);
185: Object v = factorVac.getValue();
186: BigDecimal bd = MiscHelper
187: .fromNumberToBigDecimal(v);
188: productBd = productBd.multiply(bd);
189: }
190: }
191: vacfct = ValueAndClassForConceptTag.newDecimal(productBd);
192:
193: inDvacctc.setDetermineValueAndClass(vacfct);
194: } catch (DetermineValueAndClassConceptTagException dvaccte) {
195: inDvacctc.setDetermineException(dvaccte);
196: }
197: }
198:
199: public void determineClass(DetermineClassConceptTagContext inDcctc) {
200: inDcctc.setDetermineClass(StandardDataTypes.getDecimalClass());
201: }
202:
203: public Object createUninitializedSelf() {
204: return new CommonFunctionMathMultiplyTag();
205: }
206: }
|