001: /*
002: * Copyright (C) Jakub Neubauer, 2007
003: *
004: * This file is part of TaskBlocks
005: *
006: * TaskBlocks is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 3 of the License, or
009: * (at your option) any later version.
010: *
011: * TaskBlocks is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program. If not, see <http://www.gnu.org/licenses/>.
018: */
019:
020: package taskblocks;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.w3c.dom.NodeList;
028:
029: public class Utils {
030:
031: public static final long MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
032:
033: /** The first saturday since the Epoch (time 0). */
034: public static final long FIRST_SATURDAY = 1;
035:
036: /**
037: * This method adds 'duration' working days to 'startTime'. Saturday is also the regular
038: * end of the work. (This is different from beginning of work - there saturday should be recounted
039: * to monday
040: */
041: public static long countFinishTime(long startTime, long duration) {
042: long startDayInWeek = getDayInWeek(startTime);
043:
044: long durationWeeks = (duration) / 5;
045: long durationRest = (duration) % 5;
046:
047: long daysAdd = durationWeeks * 2; // every week of work means 2 days of weekend
048:
049: if (startDayInWeek + durationRest > 5) {
050: daysAdd += 2;
051: }
052:
053: if (duration > 0 && startDayInWeek == 0 && duration % 5 == 0) {
054: daysAdd -= 2;
055: }
056: return startTime + duration + daysAdd;
057: }
058:
059: /** Returns day in week for given time. Time is the count of days, not milliseconds
060: *
061: * @param time Time in number of days.
062: * @return
063: */
064: public static int getDayInWeek(long time) {
065: return (int) ((time + FIRST_SATURDAY + 2) % 7);
066: }
067:
068: public static long repairStartTime(long startTime) {
069: long startDayInWeek = Utils.getDayInWeek(startTime);
070: if (startDayInWeek == 5) {
071: return startTime + 2;
072: } else if (startDayInWeek == 6) {
073: return startTime + 1;
074: }
075: return startTime;
076: }
077:
078: /** Counts duration between given times, counting only working days */
079: public static long countWorkDuration(long start, long end) {
080: long result;
081: long weeks = (end - start) / 7;
082: result = weeks * 5;
083: long rest = (end - start) % 7;
084: int startInWeek = getDayInWeek(start);
085: for (int i = 1; i <= rest; i++) {
086: long stepInWeek = (startInWeek + i) % 7;
087: if (stepInWeek != 0 && stepInWeek != 6) {
088: result++;
089: }
090: }
091: return result;
092: }
093:
094: public static Element[] getChilds(Element e, String name) {
095: List<Element> childs = new ArrayList<Element>();
096: NodeList nl = e.getChildNodes();
097: for (int i = 0; i < nl.getLength(); i++) {
098: Node n = nl.item(i);
099: if (n.getNodeType() == Node.ELEMENT_NODE
100: && name.equals(n.getNodeName())) {
101: childs.add((Element) n);
102: }
103: }
104: return childs.toArray(new Element[childs.size()]);
105: }
106:
107: /**
108: * Returns text enclosed in the first child element of the parent element
109: *
110: * @param parent
111: * @param childName
112: * @return
113: */
114: public static String getFirstElemText(Element parent,
115: String childName) {
116: return null; // TODO
117: }
118:
119: }
|