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.core.uidgen; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.NuxeoException; 025import org.nuxeo.ecm.core.api.PropertyException; 026import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 027import org.nuxeo.ecm.core.schema.SchemaManager; 028import org.nuxeo.ecm.core.schema.types.Schema; 029import org.nuxeo.runtime.api.Framework; 030 031/** 032 * The abstract class adds some useful methods. 033 */ 034public abstract class AbstractUIDGenerator implements UIDGenerator { 035 036 private static final Log log = LogFactory.getLog(AbstractUIDGenerator.class); 037 038 private UIDSequencer sequencer; 039 040 private String[] propertyNames; 041 042 @Override 043 public final void setSequencer(UIDSequencer sequencer) { 044 if (null == sequencer) { 045 throw new IllegalArgumentException("null sequencer"); 046 } 047 this.sequencer = sequencer; 048 } 049 050 protected int getNext(DocumentModel document) { 051 return (int) getNextLong(document); 052 } 053 054 protected long getNextLong(DocumentModel document) { 055 if (sequencer == null) { 056 throw new IllegalStateException("sequencer not defined"); 057 } 058 final String key = getSequenceKey(document); 059 assert key != null; 060 return sequencer.getNextLong(key); 061 } 062 063 @Override 064 public String getPropertyName() { 065 if (propertyNames.length == 0) { 066 log.warn("No propertyName specified"); 067 return null; 068 } 069 return propertyNames[0]; 070 } 071 072 @Override 073 public void setPropertyName(String propertyName) { 074 propertyNames = new String[] { propertyName }; 075 } 076 077 @Override 078 public void setPropertyNames(String[] propertyNames) { 079 this.propertyNames = propertyNames; 080 } 081 082 @Override 083 public String[] getPropertyNames() { 084 return propertyNames; 085 } 086 087 /** 088 * Checks if the property with the given name is defined and is not null. 089 */ 090 protected final boolean isPropValueDefined(String propName, DocumentModel document) { 091 try { 092 Object val = document.getProperty(getSchemaName(propName), getFieldName(propName)); 093 return val != null; 094 } catch (PropertyException e) { 095 return false; 096 } 097 } 098 099 protected final String str(String propName, DocumentModel document) { 100 Object val = document.getProperty(getSchemaName(propName), getFieldName(propName)); 101 if (val == null) { 102 return null; 103 } 104 if (val instanceof String) { 105 return (String) val; 106 } 107 throw new NuxeoException("Doc property '" + propName + "' is not of String type."); 108 } 109 110 @Override 111 public void setUID(DocumentModel document) throws PropertyNotFoundException { 112 String uid = createUID(document); 113 for (String propertyName : propertyNames) { 114 try { 115 document.setProperty(getSchemaName(propertyName), getFieldName(propertyName), uid); 116 } catch (PropertyNotFoundException e) { 117 e.addInfo(String.format("Cannot set uid %s on property %s for doc %s", uid, propertyName, document)); 118 throw e; 119 } 120 } 121 } 122 123 // helper method to deprecate 124 private static String getSchemaName(String propertyName) { 125 String[] s = propertyName.split(":"); 126 String prefix = s[0]; 127 SchemaManager tm = Framework.getService(SchemaManager.class); 128 Schema schema = tm.getSchemaFromPrefix(prefix); 129 if (schema == null) { 130 // fall back on prefix as it may be the schema name 131 return prefix; 132 } else { 133 return schema.getName(); 134 } 135 } 136 137 // helper method to deprecate 138 private static String getFieldName(String propertyName) { 139 String[] s = propertyName.split(":"); 140 return s[1]; 141 } 142 143}