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 CommonFunctionMathDivideTag extends
045: BaseCommonFunctionCompatibilityTag implements
046: SelfArgumentlessConstructor {
047:
048: //characteristics
049: private ConceptTagCharacteristic dividendCtchar = null;
050: private ConceptTagCharacteristic divisorCtchar = null;
051:
052: //dividend details
053: private final static String DIVIDEND_CTCON = "dividend";
054: private final static String DIVIDEND_TITLE = "Dividend";
055: private final static String DIVIDEND_SHORT_DESCRIPTION = "The number to be divided by the divisor to get the result "
056: + "('Dividend' is a fancy term for this)";
057:
058: //divisor details
059: private final static String DIVISOR_CTCON = "divisor";
060: private final static String DIVISOR_TITLE = "Divisor";
061: private final static String DIVISOR_SHORT_DESCRIPTION = "The number used to divide "
062: + "('Divisor' is a fancy term for this)";
063:
064: public void initialize(InitializeConceptTagContext inIctc) {
065:
066: //
067: ConceptTagConstraintHolder rootCtconh = ConceptTagConstraintHolder
068: .newInstance(inIctc);
069:
070: //constrain "dividend"
071: ConceptTagConstraint dividendCtcon = ConceptTagConstraint
072: .newInstance(DIVIDEND_CTCON, rootCtconh);
073: dividendCtcon.setTitle(DIVIDEND_TITLE);
074: dividendCtcon.setShortDescription(DIVIDEND_SHORT_DESCRIPTION);
075: //
076: StaticConceptTagConstraintContext dividendSctcc = StaticConceptTagConstraintContext
077: .newInstance(dividendCtcon);
078: dividendSctcc.setRegularExpression(CommonExpressions.DECIMAL);
079: //
080: DynamicConceptTagConstraintContext dividendDctcc = DynamicConceptTagConstraintContext
081: .newInstance(dividendCtcon);
082: dividendDctcc.getValidator().allowReturnOfOnlyDecimal();
083: dividendDctcc.getValidator().filterDirectChildOfRootAndHtml();
084: //
085: dividendSctcc.addDefaultValue("1");
086:
087: //constrain "divisor"
088: ConceptTagConstraint divisorCtcon = ConceptTagConstraint
089: .newInstance(DIVISOR_CTCON, rootCtconh);
090: divisorCtcon.setTitle(DIVISOR_TITLE);
091: divisorCtcon.setShortDescription(DIVISOR_SHORT_DESCRIPTION);
092: //
093: StaticConceptTagConstraintContext divisorSctcc = StaticConceptTagConstraintContext
094: .newInstance(divisorCtcon);
095: divisorSctcc.setRegularExpression(CommonExpressions.DECIMAL);
096: //
097: DynamicConceptTagConstraintContext divisorDctcc = DynamicConceptTagConstraintContext
098: .newInstance(divisorCtcon);
099: divisorDctcc.getValidator().allowReturnOfOnlyDecimal();
100: divisorDctcc.getValidator().filterDirectChildOfRootAndHtml();
101: //
102: divisorSctcc.addDefaultValue("1");
103:
104: //
105: ConceptTagFlagsHelper
106: .pushNonIterativeCommonFunctionDepth(inIctc);
107: inIctc.initialize();
108: ConceptTagFlagsHelper
109: .popNonIterativeCommonFunctionDepth(inIctc);
110:
111: //initialize characteristics
112: this .dividendCtchar = dividendCtcon.getCharacteristic();
113: this .divisorCtchar = divisorCtcon.getCharacteristic();
114: }
115:
116: public void appraise(AppraiseConceptTagContext inActc) {
117:
118: if (inActc instanceof StudioConceptTagInstanceInfoContext) {
119:
120: StudioConceptTagInstanceInfoContext sctiic = (StudioConceptTagInstanceInfoContext) inActc;
121:
122: if (this .dividendCtchar.getValueLength() == 1
123: && this .divisorCtchar.getValueLength() == 1) {
124: //initialize first operand
125: String operandTitle1 = "";
126: String operandDesc1 = "";
127: if (this .dividendCtchar.isStatic(0, inActc)) {
128: operandTitle1 = this .dividendCtchar
129: .getValueAsString(0, inActc);
130: operandDesc1 = this .dividendCtchar
131: .getValueAsString(0, inActc);
132: } else if (this .dividendCtchar.isHolderWithConceptTag(
133: 0, inActc)) {
134: //
135: StudioConceptTagInstanceInfoContext nextStiic = this .dividendCtchar
136: .getInstanceInformation(0);
137: operandTitle1 = nextStiic.getInstanceTitle();
138: operandDesc1 = nextStiic.getInstanceDescription();
139: }
140: //initialize second operand
141: String operandTitle2 = "";
142: String operandDesc2 = "";
143: if (this .divisorCtchar.isStatic(0, inActc)) {
144: operandTitle2 = this .divisorCtchar
145: .getValueAsString(0, inActc);
146: operandDesc2 = this .divisorCtchar.getValueAsString(
147: 0, inActc);
148: } else if (this .divisorCtchar.isHolderWithConceptTag(0,
149: inActc)) {
150: StudioConceptTagInstanceInfoContext nextStiic = this .divisorCtchar
151: .getInstanceInformation(0);
152: operandTitle2 = nextStiic.getInstanceTitle();
153: operandDesc2 = nextStiic.getInstanceDescription();
154: }
155:
156: //set title and description
157: sctiic.setInstanceTitle("((" + operandTitle1 + ") / ("
158: + operandTitle2 + "))");
159: sctiic.setInstanceDescription("((" + operandDesc1
160: + ") / (" + operandDesc2 + "))");
161: } else {
162: sctiic.setInstanceTitle("(Error)");
163: sctiic.setInstanceDescription("(Error)");
164: }
165: } else if (inActc instanceof StudioConceptTagTypeInfoContext) {
166:
167: StudioConceptTagTypeInfoContext scttic = (StudioConceptTagTypeInfoContext) inActc;
168:
169: scttic.setTypeTitle("Divide");
170: scttic
171: .setTypeDescription("Divides one number from another");
172: } else if (inActc instanceof WebPageTagInstanceViewContext) {
173:
174: WebPageTagInstanceViewContext wptivc = (WebPageTagInstanceViewContext) inActc;
175:
176: WebPageTagInstanceViewContextInfo info = wptivc
177: .getInfoAsNonHtml();
178: info.setNonHtmlContent("Divide - Not Impl");
179: //wptivc.getInfo().setStartContent("");
180: //wptivc.getInfo().setMiddleContent("");
181: //wptivc.getInfo().setEndContent("");
182: } else if (inActc instanceof SelectionCriteriaForReturnableConceptTag) {
183:
184: SelectionCriteriaForReturnableConceptTag scfrct = (SelectionCriteriaForReturnableConceptTag) inActc;
185:
186: scfrct.canReturnDecimal();
187: scfrct.declareFormatAsPlain();
188: } else if (inActc instanceof StudioConceptTagCreParInfoContext) {
189:
190: StudioConceptTagCreParInfoContext scpc = (StudioConceptTagCreParInfoContext) inActc;
191: }
192: }
193:
194: public void determineValueAndClass(
195: DetermineValueAndClassConceptTagContext inDvacctc) {
196:
197: try {
198: ValueAndClassForConceptTag vacfct = null;
199:
200: //ComponentsForReturnableScriptTag wcfrt =
201: // ScriptTagHelper.getComponentsForReturnableScriptTag(inRvacc);
202:
203: //dividend
204: BigDecimal dividend = null;
205: if (this .dividendCtchar.isStatic(0, inDvacctc)) {
206: dividend = new BigDecimal(this .dividendCtchar
207: .getValueAsString(0, inDvacctc));
208: } else {
209: ValueAndClassForConceptTag dividendVac = this .dividendCtchar
210: .getDetermineValueAndClass(0, inDvacctc);
211: Object v = dividendVac.getValue();
212: dividend = MiscHelper.fromNumberToBigDecimal(v);
213: }
214:
215: //divisor
216: BigDecimal divisor = null;
217: if (this .divisorCtchar.isStatic(0, inDvacctc)) {
218: divisor = new BigDecimal(this .divisorCtchar
219: .getValueAsString(0, inDvacctc));
220: } else {
221: ValueAndClassForConceptTag divisorVac = this .divisorCtchar
222: .getDetermineValueAndClass(0, inDvacctc);
223: Object v = divisorVac.getValue();
224: divisor = MiscHelper.fromNumberToBigDecimal(v);
225: }
226:
227: BigDecimal quotient = dividend.divide(divisor,
228: BigDecimal.ROUND_HALF_EVEN);
229:
230: vacfct = ValueAndClassForConceptTag.newDecimal(quotient);
231:
232: inDvacctc.setDetermineValueAndClass(vacfct);
233: } catch (DetermineValueAndClassConceptTagException dvaccte) {
234: inDvacctc.setDetermineException(dvaccte);
235: }
236: }
237:
238: public void determineClass(DetermineClassConceptTagContext inDcctc) {
239: inDcctc.setDetermineClass(StandardDataTypes.getDecimalClass());
240: }
241:
242: public Object createUninitializedSelf() {
243: return new CommonFunctionMathDivideTag();
244: }
245:
246: /*
247: public void getConceptTagSummary(ConceptTagSummaryDescriptor tsd) {
248: //TO DO: placeholder, put meaningful description
249: if (tsd.getMimeType().equals(ConceptTagSummaryMimeType.TEXT_HTML)) {
250: tsd.appendData("( ");
251: for (int i = 0; i < this.returnableConceptTags.size(); i++) {
252: if (i != 0) {
253: tsd.appendData(" / ");
254: }
255: ((ConceptTag)(this.returnableConceptTags.get(i))).
256: getConceptTagSummary(tsd);
257: }
258: tsd.appendData(" )");
259: }
260: }
261: */
262: }
|