001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.pm.assignment;
051:
052: import java.text.FieldPosition;
053: import java.text.NumberFormat;
054: import java.text.ParseException;
055: import java.text.ParsePosition;
056: import java.util.Collection;
057: import java.util.regex.Matcher;
058: import java.util.regex.Pattern;
059:
060: import com.projity.association.AssociationFormat;
061: import com.projity.association.AssociationFormatParameters;
062: import com.projity.configuration.Settings;
063: import com.projity.datatype.Rate;
064: import com.projity.datatype.RateFormat;
065: import com.projity.datatype.TimeUnit;
066: import com.projity.field.FieldParseException;
067: import com.projity.options.GeneralOption;
068: import com.projity.pm.task.Task;
069: import com.projity.pm.resource.Resource;
070: import com.projity.strings.Messages;
071:
072: public class AssignmentFormat extends AssociationFormat {
073: public static AssignmentFormat getInstance(
074: AssociationFormatParameters parameters) {
075: return new AssignmentFormat(parameters);
076: }
077:
078: private AssignmentFormat(AssociationFormatParameters parameters) {
079: super (parameters);
080: }
081:
082: private String getErrorMessage(String text) {
083: return Messages.getString("Message.invalidAssignments");
084: }
085:
086: private static NumberFormat percentFormat = NumberFormat
087: .getPercentInstance();
088:
089: private static String typePatternString = "\\s*" // optional whitespace before
090: + "(" // group1
091: + "[^"
092: + Messages.getString("Symbol.leftBracketRegex")
093: + "]+" // anything, aside from bracket
094: + ")" // end of group1
095: + "(?:" // non grouping
096: + Messages.getString("Symbol.leftBracketRegex") // open bracket
097: + "(" // group 2
098: + "\\d+" // one or more digits
099: + ".*" // room here for option percent
100: + ")" // group 2
101: + Messages.getString("Symbol.rightBracketRegex") // clost bracket
102: + ")?" + "\\s*" // optional white space
103: ;
104:
105: private static Pattern pattern = Pattern.compile(typePatternString);
106:
107: private Object doParse(String string, ParsePosition pos)
108: throws ParseException {
109: Matcher matcher = pattern.matcher(string.substring(pos
110: .getIndex()));
111: if (!matcher.matches())
112: throw new ParseException(getErrorMessage(string), pos
113: .getIndex());
114:
115: // group 1 is resource name
116: // group 2 is percent
117:
118: Object found = parameters.getIdField().find(matcher.group(1),
119: getContainer(parameters.isLeftAssociation()));
120:
121: if (found == null) {
122: if (GeneralOption.getInstance()
123: .isAutomaticallyAddNewResourcesAndTasks()) {
124: found = createNewObject(parameters.isLeftAssociation());
125:
126: if (found == null) // if couldn't create, such as trying to create a task on resource pool
127: throw new ParseException(getErrorMessage(string),
128: pos.getIndex());
129: try {
130: parameters.getIdField().setText(found,
131: matcher.group(1), null);
132: } catch (FieldParseException e) {
133: throw new ParseException(e.getMessage(), 0); //TODO don't know about this - can it happen?
134: }
135: } else {
136: throw new ParseException(getErrorMessage(string), pos
137: .getIndex());
138: }
139: }
140:
141: double percent = 1.0D;
142: Resource resource = (Resource) (parameters.isLeftAssociation() ? found
143: : parameters.getThisObject());
144: Rate rate = null;
145: if (matcher.group(2) != null) { // if text was empty use default
146: if (!getParameters().isAllowDetailsEntry())
147: throw new ParseException(Messages
148: .getString("Message.cannotEnterUnits"), 0);
149: RateFormat format = resource.getRateFormat();
150: rate = (Rate) format.parseObject(matcher.group(2));
151: percent = rate.getValue();
152: // Number percentNumber;
153: // if (resource.isLabor())
154: // percentNumber = percentFormat.parse(matcher.group(2)+ Settings.PERCENT); // force a percent sign at the end for labor. If there are two, it is ignored
155: // else //TODO allow parsing values like 3/d for material resources
156: // percentNumber = NumberFormat.getInstance().parse(matcher.group(2));
157: //
158: // if (percentNumber == null)
159: // throw new ParseException(getErrorMessage(string), pos.getIndex());
160: // percent = percentNumber.doubleValue();
161: } else if (resource.isMaterial()) {
162: rate = new Rate(1, TimeUnit.NON_TEMPORAL);
163: }
164: Assignment ass = Assignment.getInstance((Task) (parameters
165: .isLeftAssociation() ? parameters.getThisObject()
166: : found), resource, percent, 0);
167: if (rate != null)
168: ass.detail.setRate(rate);
169: return ass;
170: }
171:
172: /*
173: * (non-Javadoc)
174: *
175: * @see java.text.Format#parseObject(java.lang.String,
176: * java.text.ParsePosition)
177: */
178: public Object parseObject(String string, ParsePosition pos) {
179: try {
180: return doParse(string, pos);
181: } catch (ParseException e) {
182: parameters.setError(e.getMessage());
183: return null;
184: }
185: }
186:
187: /**
188: * convert to text. The format is either, John or John[50%]
189: */
190: public StringBuffer format(Object assignmentObject,
191: StringBuffer string, FieldPosition fieldPos) {
192: Assignment assignment = (Assignment) assignmentObject;
193: Object showObject = ((parameters.isLeftAssociation()) ? (Object) assignment
194: .getResource()
195: : (Object) assignment.getTask());
196: string.append(parameters.getIdField()
197: .getValue(showObject, null));
198: if (parameters.isEncloseInBrackets()) {
199: double units = assignment.getUnits();
200: if (units != 1D) {
201: string.append(Settings.LEFT_BRACKET);
202: string.append(assignment.getRateFormat().format(
203: assignment.getRate()));
204: string.append(Settings.RIGHT_BRACKET);
205: }
206: }
207: return string;
208: }
209:
210: /* (non-Javadoc)
211: * @see com.projity.association.AssociationFormat#getContainer(boolean)
212: */
213: protected Collection getContainer(boolean left) {
214: if (left)
215: return ((Task) parameters.getThisObject()).getProject()
216: .getResourcePool().getResourceList();
217: else
218: return null; // TODO if we implement projet-specific resource pools, we can handle this
219: }
220:
221: protected Object createNewObject(boolean left) {
222: if (left)
223: return ((Task) parameters.getThisObject()).getProject()
224: .getResourcePool().newResourceInstance();
225: else
226: return null; // TODO if we implement projet-specific resource pools, we can handle this
227: }
228:
229: }
|