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.util.Calendar;
027    import java.util.GregorianCalendar;
028    
029    /**
030     * Builds a new, unique NISS based on the current date and time expressed
031     * in seconds. The resulting NISS is non-speaking, but unique and somewhat
032     * optimized for the nbn:de checksum algorithm. Only one NISS per second 
033     * will be generated.
034     * 
035     * @author Frank Lützenkirchen
036     */
037    public class MCRNISSBuilderFL implements MCRNISSBuilder {
038        private String last;
039    
040        public void init(String configID) {
041        }
042    
043        public synchronized String buildNISS() {
044            Calendar now = new GregorianCalendar();
045            int yyy = 2268 - now.get(Calendar.YEAR);
046            int ddd = 500 - now.get(Calendar.DAY_OF_YEAR);
047            int hh = now.get(Calendar.HOUR_OF_DAY);
048            int mm = now.get(Calendar.MINUTE);
049            int ss = now.get(Calendar.SECOND);
050            int sss = 99999 - ((hh * 3600) + (mm * 60) + ss);
051    
052            String DDDDD = String.valueOf((yyy * 366) + ddd);
053    
054            StringBuffer buffer = new StringBuffer();
055            buffer.append(DDDDD.charAt(4));
056            buffer.append(DDDDD.charAt(2));
057            buffer.append(DDDDD.charAt(1));
058            buffer.append(DDDDD.charAt(3));
059            buffer.append(DDDDD.charAt(0));
060            buffer.append(sss);
061            String niss = buffer.toString();
062    
063            if (niss.equals(last)) {
064                try {
065                    Thread.sleep(500);
066                } catch (InterruptedException ignored) {
067                }
068    
069                return buildNISS();
070            } else {
071                last = niss;
072                return niss;
073            }
074        }
075    }