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 public UIDSequenceBean(String key) { 064 this.key = key; 065 index = 0; 066 } 067 068 public int getId() { 069 return id; 070 } 071 072 public void setId(int id) { 073 this.id = id; 074 } 075 076 public String getKey() { 077 return key; 078 } 079 080 public void setKey(String key) { 081 this.key = key; 082 } 083 084 public int getIndex() { 085 return index; 086 } 087 088 public static String stringify(UIDSequenceBean bean) { 089 return "UIDSeq(" + bean.key + "," + bean.index + ")"; 090 } 091 092 @Override 093 public String toString() { 094 return stringify(this); 095 } 096 097 public int nextIndex() { 098 index += 1; 099 log.debug("updated to " + this); 100 return index; 101 } 102 103}