001 /**
002 *
003 * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004 *
005 * This file is part of ** M y C o R e **
006 * Visit our homepage at http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, normally in the file license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 *
023 **/
024 package org.mycore.services.urn;
025
026 import java.text.DecimalFormat;
027 import java.text.SimpleDateFormat;
028 import java.util.Calendar;
029 import java.util.GregorianCalendar;
030
031 import org.mycore.common.MCRConfiguration;
032
033 /**
034 * Builds a new, unique NISS based on the current date and/or time
035 * in combination with a counter. The date/time can be formatted with
036 * a Java SimpleDateFormat pattern, the counter ca be formatted with
037 * a Java DecimalFormat pattern. The property "NISSPattern" is used
038 * for configuring the instance. Example configuration:
039 *
040 * MCR.URN.SubNamespace.Essen.Prefix=urn:nbn:de:465-miless-
041 * MCR.URN.SubNamespace.Essen.NISSBuilder=org.mycore.services.urn.MCRNISSBuilderDateCounter
042 * MCR.URN.SubNamespace.Essen.NISSPattern=yyyyMMdd-HHmmss-000
043 *
044 * Subsequent calls to MCRURN.buildURN( "Essen" ) could then generate
045 * the following URNs, for example:
046 *
047 * urn:nbn:de:465-miless-20060622-213404-0017
048 * urn:nbn:de:465-miless-20060622-213404-0025
049 * urn:nbn:de:465-miless-20060622-213448-0013
050 *
051 * The last character is the checksum digit.
052 * In the first two URNs, the generated date pattern is the same, so
053 * the counter is increased (starting at 1). The use of "0" instead of
054 * "#" in the pattern produces leading zeros.
055 *
056 * A pattern might have no date part (only use counter)
057 * or no counter part (only use date pattern)
058 *
059 * @author Frank Lützenkirchen
060 */
061 public class MCRNISSBuilderDateCounter implements MCRNISSBuilder {
062
063 private String lastDate;
064
065 private String lastNISS;
066
067 private int counter = 1;
068
069 private SimpleDateFormat fmtDate;
070
071 private DecimalFormat fmtCount;
072
073 public void init(String configID) {
074 String property = "MCR.URN.SubNamespace." + configID + ".NISSPattern";
075 String pattern = MCRConfiguration.instance().getString(property);
076 String patternDate = pattern;
077 String patternCounter = "";
078
079 int pos1 = pattern.indexOf("0");
080 int pos2 = pattern.indexOf("#");
081 if ((pos1 >= 0) || (pos2 >= 0)) {
082 int pos;
083
084 if (pos1 == -1)
085 pos = pos2;
086 else if (pos2 == -1)
087 pos = pos1;
088 else
089 pos = Math.min(pos1, pos2);
090
091 patternDate = pattern.substring(0, pos);
092 patternCounter = pattern.substring(pos);
093 }
094
095 if (patternDate.length() > 0)
096 fmtDate = new SimpleDateFormat(patternDate);
097
098 if (patternCounter.length() > 0)
099 fmtCount = new DecimalFormat(patternCounter);
100 }
101
102 public synchronized String buildNISS() {
103 String niss;
104
105 do {
106 StringBuffer sb = new StringBuffer();
107
108 if (fmtDate != null) {
109 Calendar now = new GregorianCalendar();
110 String date = fmtDate.format(now.getTime());
111 sb.append(date);
112
113 if (!date.equals(lastDate)) {
114 lastDate = date;
115 counter = 1; // reset counter, new date
116 }
117 }
118
119 if (fmtCount != null) {
120 sb.append(fmtCount.format(counter++));
121 }
122
123 niss = sb.toString();
124 } while (niss.equals(lastNISS));
125
126 lastNISS = niss;
127 return niss;
128 }
129 }