|
From: <j_m...@us...> - 2004-02-29 12:21:46
|
Update of /cvsroot/cobricks/cobricks2/src/org/cobricks/item In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11169/src/org/cobricks/item Modified Files: ItemManagerDB.java Log Message: Index: ItemManagerDB.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/item/ItemManagerDB.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ItemManagerDB.java 21 Feb 2004 18:49:12 -0000 1.4 --- ItemManagerDB.java 29 Feb 2004 12:03:42 -0000 1.5 *************** *** 6,10 **** import org.cobricks.core.OntologyClass; import org.cobricks.core.OntologyClassAttr; ! /** * @author Johannes Mathes ma...@in... --- 6,11 ---- import org.cobricks.core.OntologyClass; import org.cobricks.core.OntologyClassAttr; ! import org.cobricks.core.OntologyDataType; ! import org.cobricks.core.DataObject; /** * @author Johannes Mathes ma...@in... *************** *** 20,24 **** import java.util.Map; import java.util.Set; ! /** --- 21,25 ---- import java.util.Map; import java.util.Set; ! import org.xmldb.api.base.XMLDBException; /** *************** *** 73,114 **** * @param attribute The attribute to store. * @param name The name of the attribute. ! * @param itemclasses The itemclasses which correspond to the current * item ! * @param itemid The itemid of item from which the attribute is part of */ protected final void storeAttribute(Object attribute, String name, ! OntologyClass[] itemclasses, int itemid) { //Default values ! String ontKeyWord = ""; // The whole keyword - that means e.g. string[] ! String ontKeyName = "string"; // The name - e.g. string ! boolean hasBrackets = false; String contextClass = null; //Lookup all ontology-related stuff ! if (ItemAttrs.getOCAttr(itemclasses, name) != null) { ! OntologyClassAttr oca = ItemAttrs.getOCAttr(itemclasses, name); ! ontKeyWord = oca.getType(); ! ! Map tokenized = ItemAttrs.tokenizeOntologyKeyWord(ontKeyWord); ! ontKeyName = (String) tokenized.get("keyWordName"); ! hasBrackets = ! ((Boolean) tokenized.get("hasBrackets")).booleanValue(); contextClass = oca.getContextClassName(); } ! //See if it is a special attribute, this lookup is done with the ont // key name ! boolean isSpecial = ItemAttrs.attributeIsSpecialType(ontKeyName); ! ! if (hasBrackets) { // At this time, only sets of special attributes are stored // set entry for set entry ! this.storeCollections(attribute, name, ontKeyName, contextClass, ! itemid, isSpecial); } else { //All single attributes and all Sets of non-special attributes // are stored as a single attribute ! this.storeSingleAttribute(attribute, name, ontKeyName, ! contextClass, itemid, isSpecial); } } --- 74,106 ---- * @param attribute The attribute to store. * @param name The name of the attribute. ! * @param itemclass The item class which correspond to the current * item ! * @param itemid The item id of item from which the attribute is part of */ protected final void storeAttribute(Object attribute, String name, ! OntologyClass itemclass, int itemid) { //Default values ! OntologyDataType ontType = new OntologyDataType("string"); String contextClass = null; //Lookup all ontology-related stuff ! if (itemclass.getAttribute(name) != null) { ! OntologyClassAttr oca = itemclass.getAttribute(name); ! ontType = oca.getOntologyDataType(); contextClass = oca.getContextClassName(); } ! //See if it is a special attribute, this lookup is done with the ontology // key name ! if (ontType.isCollection()) { // At this time, only sets of special attributes are stored // set entry for set entry ! this.storeCollections(attribute, name, ontType, contextClass, ! itemid); } else { //All single attributes and all Sets of non-special attributes // are stored as a single attribute ! this.storeSingleAttribute(attribute, name, ontType, ! contextClass, itemid); } } *************** *** 121,156 **** * @param oldAttribute Maybe it is necessary to compare with the old attribute * @param name The name of the attribute ! * @param itemclasses The ontology information is needed to get the type ! * of attribute and perhaps contextclass information. * @param itemid The id of the item this attribute belongs to */ protected final void updateAttribute(Object attribute, Object oldAttribute, ! String name, OntologyClass[] itemclasses, int itemid) { ! //The type of the attribute, according to the itemclass ! String ontKeyWord = ""; ! String ontKeyName = "string"; ! boolean hasBrackets = false; String contextClass = null; ! if (ItemAttrs.getOCAttr(itemclasses, name) != null) { ! OntologyClassAttr oca = ItemAttrs.getOCAttr(itemclasses, name); ! ontKeyWord = oca.getType(); ! ! Map tokenized = ItemAttrs.tokenizeOntologyKeyWord(ontKeyWord); ! ontKeyName = (String) tokenized.get("keyWordName"); ! hasBrackets = ! ((Boolean) tokenized.get("hasBrackets")).booleanValue(); contextClass = oca.getContextClassName(); } ! ! boolean isSpecial = ItemAttrs.attributeIsSpecialType(ontKeyName); ! ! if (hasBrackets) { this.updateCollections((Collection) attribute, ! (Collection) oldAttribute, name, ontKeyName, contextClass, ! itemid, isSpecial); } else { ! this.updateSingleAttribute(attribute, name, ontKeyName, ! contextClass, itemid, isSpecial); } } --- 113,138 ---- * @param oldAttribute Maybe it is necessary to compare with the old attribute * @param name The name of the attribute ! * @param itemclass The ontology information is needed to get the type ! * of attribute and perhaps context class information. * @param itemid The id of the item this attribute belongs to */ protected final void updateAttribute(Object attribute, Object oldAttribute, ! String name, OntologyClass itemclass, int itemid) { ! //The type of the attribute, according to the item class ! OntologyDataType ontType = new OntologyDataType("string"); String contextClass = null; ! if (itemclass.getAttribute(name) != null) { ! OntologyClassAttr oca = itemclass.getAttribute(name); ! ontType = oca.getOntologyDataType(); contextClass = oca.getContextClassName(); } ! if (ontType.isCollection()) { this.updateCollections((Collection) attribute, ! (Collection) oldAttribute, name, ontType, ! contextClass, itemid); } else { ! this.updateSingleAttribute(attribute, name, ontType, ! contextClass, itemid); } } *************** *** 163,171 **** * @param oldItem The old item (the one which is in the database) * @param newItem The new item (the one which has changed) ! * @param itemclasses The itemclasses which correspond to this item */ protected final void updateAttributeXMLDB(String name, Item oldItem, ! Item newItem, OntologyClass[] itemclasses) { String xpath = "/item[globalid='" + newItem.getGlobalId() + "']"; --- 145,153 ---- * @param oldItem The old item (the one which is in the database) * @param newItem The new item (the one which has changed) ! * @param itemclass The itemclass which correspond to this item */ protected final void updateAttributeXMLDB(String name, Item oldItem, ! Item newItem, OntologyClass itemclass) throws XMLDBException { String xpath = "/item[globalid='" + newItem.getGlobalId() + "']"; *************** *** 173,195 **** logger.debug("Try to update attribute: " + name + "itemid :" + String.valueOf(oldItem.getLocalId())); ! ! String itemclass = newItem.getItemClass(); ! ! if (itemclass == null) { ! itemclass = "item"; ! } ! ! OntologyClassAttr oca = ItemAttrs.getOCAttr(itemclasses, name); ! String attributeType = null; ! ! if (oca == null) { ! attributeType = "custom"; ! } else { ! attributeType = oca.getType(); ! } String oldXML = oldItem.getItemAttributeAsXML(name, false); String newXML = newItem.getItemAttributeAsXML(name, false); xmlDBAccess.update(oldXML, newXML, xpath); } --- 155,164 ---- logger.debug("Try to update attribute: " + name + "itemid :" + String.valueOf(oldItem.getLocalId())); ! OntologyClassAttr oca = itemclass.getAttribute(name); String oldXML = oldItem.getItemAttributeAsXML(name, false); String newXML = newItem.getItemAttributeAsXML(name, false); xmlDBAccess.update(oldXML, newXML, xpath); + } *************** *** 212,247 **** * This method deletes a single attribute-value pair in the rel. db * @param name The name of the attribute ! * @param itemclasses The itemclasses (all parent item classes and the custom ! * item class) of the item the attribute is from * @param itemid The itemid of the item the attribute is from */ protected final void removeAttribute(String name, ! OntologyClass[] itemclasses, int itemid) { //The type of the attribute, according to the itemclass ! String ontKeyWord = ""; ! String ontKeyName = "string"; ! boolean hasBrackets = false; String contextClass = null; ! if (ItemAttrs.getOCAttr(itemclasses, name) != null) { ! OntologyClassAttr oca = ItemAttrs.getOCAttr(itemclasses, name); ! ontKeyWord = oca.getType(); ! ! Map tokenized = ItemAttrs.tokenizeOntologyKeyWord(ontKeyWord); ! ontKeyName = (String) tokenized.get("keyWordName"); ! hasBrackets = ! ((Boolean) tokenized.get("hasBrackets")).booleanValue(); contextClass = oca.getContextClassName(); } //Don't uses prepareRecordSet because attribute value is not submitted //(and not necessary) ! boolean isSpecial = ItemAttrs.attributeIsSpecialType(ontKeyWord); Map toDelete = new HashMap(); toDelete.put("itemid", new Integer(itemid)); ! if (isSpecial) { toDelete.put("aname", name); ! this.dbAccess.sqlDelete("item_attrs" + ontKeyName, toDelete); } else if (name.matches("(content|title)_..") || name.matches("(content|title)")) { --- 181,209 ---- * This method deletes a single attribute-value pair in the rel. db * @param name The name of the attribute ! * @param itemclass The itemclass of the item the attribute is from * @param itemid The itemid of the item the attribute is from */ protected final void removeAttribute(String name, ! OntologyClass itemclass, int itemid) { //The type of the attribute, according to the itemclass ! OntologyDataType ontType = new OntologyDataType("string"); String contextClass = null; ! if (itemclass.getAttribute(name) != null) { ! OntologyClassAttr oca = itemclass.getAttribute(name); ! ontType = oca.getOntologyDataType(); contextClass = oca.getContextClassName(); } //Don't uses prepareRecordSet because attribute value is not submitted //(and not necessary) ! //boolean isSpecial = ItemAttrs.attributeIsSpecialType(ontType); Map toDelete = new HashMap(); toDelete.put("itemid", new Integer(itemid)); ! if (ontType.isCobricksType()) { toDelete.put("aname", name); ! this.dbAccess.sqlDelete("item_attrs" + ontType.getTypeName(), ! toDelete); } else if (name.matches("(content|title)_..") || name.matches("(content|title)")) { *************** *** 322,325 **** --- 284,297 ---- return new Item(xml, itemManager); } + /** + * This method performs an update by removing the whole old item and inserting + * the new one. Use only in emergency. + * @param item The new item to update. + */ + protected final void performSafeUpdate(Item item) { + logger.info("Performing safe update for item: " + item.getLocalId()); + this.xmlDBAccess.updateWholeDocument(item.getItemAsXML(false, false), + String.valueOf(item.getLocalId())); + } /** *************** *** 328,343 **** * @param attribute The attribute to store * @param name The name of the attribute ! * @param ontKeyName The ontology key name (NOT the whole key word) ! * @param contextClass The contextclass of this attribute (only * in case it is special attribute) ! * @param itemid The itemid this attribute belongs to ! * @param isSpecial Is it a special attribute? */ private void storeSingleAttribute(Object attribute, String name, ! String ontKeyName, String contextClass, int itemid, boolean isSpecial) { Map recordset = ! this.prepareRecordSet(attribute, name, ontKeyName, contextClass, ! itemid, isSpecial); ! String tablename = this.findTableName(name, ontKeyName, isSpecial); this.dbAccess.sqlInsert(tablename, recordset); --- 300,314 ---- * @param attribute The attribute to store * @param name The name of the attribute ! * @param ontDataType The ontology data type of this attribute ! * @param contextClass The context class of this attribute (only * in case it is special attribute) ! * @param itemid The item id this attribute belongs to */ private void storeSingleAttribute(Object attribute, String name, ! OntologyDataType ontDataType, String contextClass, int itemid) { Map recordset = ! this.prepareRecordSet(attribute, name, ontDataType, contextClass, ! itemid); ! String tablename = this.findTableName(name, ontDataType); this.dbAccess.sqlInsert(tablename, recordset); *************** *** 375,394 **** * @param attribute The attribute to store * @param name The attribute name. ! * @param ontKeyName The ontology key name (not the whole keyword). * @param contextClass The ontology contextclass (in case there is one, * otherwise null) * @param itemid The local id of the corresponding item - * @param isSpecial Is it a special attribute? (Avoids another isSpecial - * test). */ private void updateSingleAttribute(Object attribute, String name, ! String ontKeyName, String contextClass, int itemid, boolean isSpecial) { Map recordset = ! this.prepareRecordSet(attribute, name, ontKeyName, contextClass, ! itemid, isSpecial); String whereCondition = "itemid=" + String.valueOf(itemid); recordset.remove("itemid"); ! if (isSpecial) { whereCondition = whereCondition + " and aname LIKE " + "\"" + name + "\""; --- 346,363 ---- * @param attribute The attribute to store * @param name The attribute name. ! * @param ontType The ontology data type of this attribute * @param contextClass The ontology contextclass (in case there is one, * otherwise null) * @param itemid The local id of the corresponding item */ private void updateSingleAttribute(Object attribute, String name, ! OntologyDataType ontType, String contextClass, int itemid) { Map recordset = ! this.prepareRecordSet(attribute, name, ontType, contextClass, ! itemid); String whereCondition = "itemid=" + String.valueOf(itemid); recordset.remove("itemid"); ! if (ontType.isCobricksType()) { whereCondition = whereCondition + " and aname LIKE " + "\"" + name + "\""; *************** *** 402,406 **** } ! String tablename = this.findTableName(name, ontKeyName, isSpecial); this.dbAccess.sqlUpdate(tablename, recordset, whereCondition); } --- 371,375 ---- } ! String tablename = this.findTableName(name, ontType); this.dbAccess.sqlUpdate(tablename, recordset, whereCondition); } *************** *** 412,430 **** * @param attribute The collection of attributes to store * @param name The attribute name. ! * @param ontKeyName The ontlogy name of the type of attribute to store. * @param contextClass The contextclass (in case there is one, otherwise * null). * @param itemid The local id of the corresponding item - * @param isSpecial Is it a special attribute? Used to avoid another - * isSpecial query. */ private void storeCollections(Object attribute, String name, ! String ontKeyName, String contextClass, int itemid, boolean isSpecial) { //if (isSpecial) { Iterator it = ((Collection) attribute).iterator(); while (it.hasNext()) { ! this.storeSingleAttribute(it.next(), name, ontKeyName, ! contextClass, itemid, isSpecial); } --- 381,398 ---- * @param attribute The collection of attributes to store * @param name The attribute name. ! * @param ontType The ontlogy name of the type of attribute to store. * @param contextClass The contextclass (in case there is one, otherwise * null). * @param itemid The local id of the corresponding item */ + private void storeCollections(Object attribute, String name, ! OntologyDataType ontType, String contextClass, int itemid) { //if (isSpecial) { Iterator it = ((Collection) attribute).iterator(); while (it.hasNext()) { ! this.storeSingleAttribute(it.next(), name, ontType, ! contextClass, itemid); } *************** *** 446,462 **** * of course the content should be the same type as the new attributes * @param name The attribute name ! * @param ontKeyName The ontology key name (not the whole keyword) ! * @param contextClass The contextclass in case it is a special attribute, * null otherwise * @param itemid The local id of the item - * @param isSpecial In order to avoid another isSpecial test, it has to be - * specified whether a normal or a special attribute is submitted. */ private void updateCollections(java.util.Collection attribute, ! java.util.Collection oldAttribute, String name, String ontKeyName, ! String contextClass, int itemid, boolean isSpecial) { ! if (isSpecial) { ! Map oldO = ItemAttrs.createIDMap((Set) oldAttribute, ontKeyName); ! Map newO = ItemAttrs.createIDMap((Set) attribute, ontKeyName); Map diff = ItemAttrs.compareCols(oldO.keySet(), newO.keySet()); Set added = (Set) diff.get("added"); //contains the anames of added --- 414,430 ---- * of course the content should be the same type as the new attributes * @param name The attribute name ! * @param ontType The ontology data type of this attribute. ! * @param contextClass The context class in case it is a special attribute, * null otherwise * @param itemid The local id of the item */ private void updateCollections(java.util.Collection attribute, ! java.util.Collection oldAttribute, String name, OntologyDataType ontType, ! String contextClass, int itemid) { ! if (ontType.isCobricksType()) { ! Map oldO = ItemAttrs.createIDMap((Set) oldAttribute, ! ontType.getTypeName()); ! Map newO = ItemAttrs.createIDMap((Set) attribute, ! ontType.getTypeName()); Map diff = ItemAttrs.compareCols(oldO.keySet(), newO.keySet()); Set added = (Set) diff.get("added"); //contains the anames of added *************** *** 467,471 **** while (it.hasNext()) { this.storeSingleAttribute(newO.get(it.next()), name, ! ontKeyName, contextClass, itemid, isSpecial); } --- 435,439 ---- while (it.hasNext()) { this.storeSingleAttribute(newO.get(it.next()), name, ! ontType, contextClass, itemid); } *************** *** 474,478 **** while (it.hasNext()) { this.removeSingleAttribute(oldO.get(it.next()), name, ! ontKeyName, itemid, true); } } else { --- 442,446 ---- while (it.hasNext()) { this.removeSingleAttribute(oldO.get(it.next()), name, ! ontType, itemid); } } else { *************** *** 486,491 **** while (it.hasNext()) { ! this.storeSingleAttribute(it.next(), name, ontKeyName, ! contextClass, itemid, false); } --- 454,459 ---- while (it.hasNext()) { ! this.storeSingleAttribute(it.next(), name, ! ontType.getCollectionElementType(), contextClass, itemid); } *************** *** 493,498 **** while (it.hasNext()) { ! this.removeSingleAttribute(it.next(), name, ontKeyName, itemid, ! false); } } --- 461,466 ---- while (it.hasNext()) { ! this.removeSingleAttribute(it.next(), name, ! ontType.getCollectionElementType(), itemid); } } *************** *** 502,514 **** * This method finds out the correct table name for a specific attribute * @param aname The name of the attribute to store in the RDB ! * @param ontKeyName The ontology name of the attribute type ! * @param isSpecial Is it a special attribute? Avoids additional isSpecial ! * test * @return The name of the table where to store */ ! private String findTableName(String aname, String ontKeyName, ! boolean isSpecial) { ! if (isSpecial) { ! return ("item_attrs" + ontKeyName); } else if (aname.matches("(content|title)_..") || aname.matches("(content|title)")) { --- 470,479 ---- * This method finds out the correct table name for a specific attribute * @param aname The name of the attribute to store in the RDB ! * @param ontType The ontology data type of this attribute. * @return The name of the table where to store */ ! private String findTableName(String aname, OntologyDataType ontType) { ! if (ontType.isCobricksType()) { ! return ("item_attrs" + ontType.getTypeName()); } else if (aname.matches("(content|title)_..") || aname.matches("(content|title)")) { *************** *** 530,543 **** /** * This method prepares the map containing all necessary information for a ! * sql command (e.g. insert or update). The only other thing to take care of * is to find the correct table. * @param attribute The attribute value. * @param name The attribute name. ! * @param ontKeyName The ontology key name - NOT the whole keyword. * @param contextClass The context class (in case there is one, otherwise * null) ! * @param itemid The itemid. ! * @param isSpecial Additional information for avoiding additional isSpecial ! * test * @return Map containing all necessary information to make an insert state- * ment, depends on type: itemid, aname, contextclass (special), --- 495,506 ---- /** * This method prepares the map containing all necessary information for a ! * SQL command (e.g. insert or update). The only other thing to take care of * is to find the correct table. * @param attribute The attribute value. * @param name The attribute name. ! * @param ontType The ontology data type. * @param contextClass The context class (in case there is one, otherwise * null) ! * @param itemid The item id. * @return Map containing all necessary information to make an insert state- * ment, depends on type: itemid, aname, contextclass (special), *************** *** 546,550 **** */ private Map prepareRecordSet(Object attribute, String name, ! String ontKeyName, String contextClass, int itemid, boolean isSpecial) { Map recordset = new HashMap(); String whereCondition = "itemid=" + String.valueOf(itemid); --- 509,513 ---- */ private Map prepareRecordSet(Object attribute, String name, ! OntologyDataType ontType, String contextClass, int itemid) { Map recordset = new HashMap(); String whereCondition = "itemid=" + String.valueOf(itemid); *************** *** 554,563 **** Integer id; ! if (isSpecial) { recordset.put("aname", name); recordset.put("contextclass", contextClass); ! id = new Integer(ItemAttrs.getId(attribute, ontKeyName)); ! recordset.put("a" + ontKeyName + "id", id); /*if (!isUpdate) { --- 517,526 ---- Integer id; ! if (ontType.isCobricksType()) { recordset.put("aname", name); recordset.put("contextclass", contextClass); ! id = new Integer(((DataObject) attribute).getId()); ! recordset.put("a" + ontType.getTypeName() + "id", id); /*if (!isUpdate) { *************** *** 612,629 **** * attributes with same aname and itemid (list) * @param aname The aname of the attribute to delete ! * @param ontKeyName The name (NOT the whole keyword). Needed to match * to the correct table. * @param itemid The item as key needed. - * @param isSpecial Giving this parameter speeds up the determing if it is - * a special parameter or not. */ private void removeSingleAttribute(Object attribute, String aname, ! String ontKeyName, int itemid, boolean isSpecial) { Map toDelete = ! this.prepareRecordSet(attribute, aname, ontKeyName, null, itemid, ! isSpecial); toDelete.remove("contextclass"); ! String tablename = this.findTableName(aname, ontKeyName, isSpecial); this.dbAccess.sqlDelete(tablename, toDelete); } --- 575,589 ---- * attributes with same aname and itemid (list) * @param aname The aname of the attribute to delete ! * @param ontType The ontology data type. Needed to match * to the correct table. * @param itemid The item as key needed. */ private void removeSingleAttribute(Object attribute, String aname, ! OntologyDataType ontType, int itemid) { Map toDelete = ! this.prepareRecordSet(attribute, aname, ontType, null, itemid); toDelete.remove("contextclass"); ! String tablename = this.findTableName(aname, ontType); this.dbAccess.sqlDelete(tablename, toDelete); } |