001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Dragos Mihalache 018 */ 019package org.nuxeo.ecm.platform.uidgen; 020 021import javax.persistence.Column; 022import javax.persistence.Entity; 023import javax.persistence.GeneratedValue; 024import javax.persistence.GenerationType; 025import javax.persistence.Id; 026import javax.persistence.NamedQueries; 027import javax.persistence.NamedQuery; 028import javax.persistence.Table; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032 033/** 034 * UID entity - keeps last indexes of all generated UIDs. 035 */ 036@Entity 037@NamedQueries({ @NamedQuery(name = "UIDSequence.findByKey", query = "from UIDSequenceBean seq where seq.key = :key") }) 038@Table(name = "NXP_UIDSEQ") 039public class UIDSequenceBean { 040 041 public static final Log log = LogFactory.getLog(UIDSequenceBean.class); 042 043 @Id 044 @Column(name = "SEQ_ID", nullable = false) 045 @GeneratedValue(strategy = GenerationType.AUTO) 046 protected int id; 047 048 @Column(name = "SEQ_KEY", nullable = false, unique = true) 049 private String key; 050 051 @Column(name = "SEQ_INDEX", nullable = false) 052 private int index; 053 054 /** 055 * Default constructor needed for EJB container instantiation. 056 */ 057 public UIDSequenceBean() { 058 } 059 060 /** 061 * Constructor taking as argument the key for which this sequence is created. The index is defaulted to 1. 062 * 063 * @param key 064 */ 065 public UIDSequenceBean(String key) { 066 this.key = key; 067 index = 0; 068 } 069 070 public int getId() { 071 return id; 072 } 073 074 public void setId(int id) { 075 this.id = id; 076 } 077 078 public String getKey() { 079 return key; 080 } 081 082 public void setKey(String key) { 083 this.key = key; 084 } 085 086 public int getIndex() { 087 return index; 088 } 089 090 public static String stringify(UIDSequenceBean bean) { 091 return "UIDSeq(" + bean.key + "," + bean.index + ")"; 092 } 093 094 @Override 095 public String toString() { 096 return stringify(this); 097 } 098 099 public int nextIndex() { 100 index += 1; 101 log.debug("updated to " + this); 102 return index; 103 } 104 105}