/** * Creates a new metadata field. * * @param context DSpace context object * @throws IOException * @throws AuthorizeException * @throws SQLException * @throws NonUniqueMetadataException */ public void create(Context context) throws IOException, AuthorizeException, SQLException, NonUniqueMetadataException { // Check authorisation: Only admins may create DC types if (!AuthorizeManager.isAdmin(context)) { throw new AuthorizeException("Only administrators may modify the metadata registry"); } // Ensure the element and qualifier are unique within a given schema. if (!unique(context, schemaID, element, qualifier)) { throw new NonUniqueMetadataException( "Please make " + element + "." + qualifier + " unique within schema #" + schemaID); } // Create a table row and update it with the values row = DatabaseManager.row("MetadataFieldRegistry"); row.setColumn("metadata_schema_id", schemaID); row.setColumn("element", element); row.setColumn("qualifier", qualifier); row.setColumn("scope_note", scopeNote); DatabaseManager.insert(context, row); decache(); // Remember the new row number this.fieldID = row.getIntColumn("metadata_field_id"); log.info( LogManager.getHeader( context, "create_metadata_field", "metadata_field_id=" + row.getIntColumn("metadata_field_id"))); }
/** * Return the workflow item to the workspace of the submitter. The workflow item is removed, and a * workspace item created. * * @param c Context * @param wfi WorkflowItem to be 'dismantled' * @return the workspace item */ private static WorkspaceItem returnToWorkspace(Context c, WorkflowItem wfi) throws SQLException, IOException, AuthorizeException { Item myitem = wfi.getItem(); Collection mycollection = wfi.getCollection(); // FIXME: How should this interact with the workflow system? // FIXME: Remove license // FIXME: Provenance statement? // Create the new workspace item row TableRow row = DatabaseManager.create(c, "workspaceitem"); row.setColumn("item_id", myitem.getID()); row.setColumn("collection_id", mycollection.getID()); DatabaseManager.update(c, row); int wsi_id = row.getIntColumn("workspace_item_id"); WorkspaceItem wi = WorkspaceItem.find(c, wsi_id); wi.setMultipleFiles(wfi.hasMultipleFiles()); wi.setMultipleTitles(wfi.hasMultipleTitles()); wi.setPublishedBefore(wfi.isPublishedBefore()); wi.update(); // myitem.update(); log.info( LogManager.getHeader( c, "return_to_workspace", "workflow_item_id=" + wfi.getID() + "workspace_item_id=" + wi.getID())); // Now remove the workflow object manually from the database DatabaseManager.updateQuery(c, "DELETE FROM WorkflowItem WHERE workflow_id=" + wfi.getID()); return wi; }
private void populateTableRowFromEPerson(EPerson eperson, TableRow row) { for (EPersonMetadataField f : EPersonMetadataField.values()) { row.setColumn(f.toString(), eperson.getMetadata(f)); } row.setColumn("can_log_in", eperson.canLogIn()); row.setColumn("require_certificate", eperson.getRequireCertificate()); row.setColumn("self_registered", eperson.getSelfRegistered()); }
/** Update the group - writing out group object and EPerson list if necessary */ public void update() throws SQLException, AuthorizeException { // FIXME: Check authorisation DatabaseManager.update(myContext, myRow); if (modifiedMetadata) { myContext.addEvent(new Event(Event.MODIFY_METADATA, Constants.GROUP, getID(), getDetails())); modifiedMetadata = false; clearDetails(); } // Redo eperson mappings if they've changed if (epeopleChanged) { // Remove any existing mappings DatabaseManager.updateQuery( myContext, "delete from epersongroup2eperson where eperson_group_id= ? ", getID()); // Add new mappings Iterator<EPerson> i = epeople.iterator(); while (i.hasNext()) { EPerson e = i.next(); TableRow mappingRow = DatabaseManager.row("epersongroup2eperson"); mappingRow.setColumn("eperson_id", e.getID()); mappingRow.setColumn("eperson_group_id", getID()); DatabaseManager.insert(myContext, mappingRow); } epeopleChanged = false; } // Redo Group mappings if they've changed if (groupsChanged) { // Remove any existing mappings DatabaseManager.updateQuery( myContext, "delete from group2group where parent_id= ? ", getID()); // Add new mappings Iterator<Group> i = groups.iterator(); while (i.hasNext()) { Group g = i.next(); TableRow mappingRow = DatabaseManager.row("group2group"); mappingRow.setColumn("parent_id", getID()); mappingRow.setColumn("child_id", g.getID()); DatabaseManager.insert(myContext, mappingRow); } // groups changed, now change group cache rethinkGroupCache(); groupsChanged = false; } log.info(LogManager.getHeader(myContext, "update_group", "group_id=" + getID())); }
// creates workflow tasklist entries for a workflow // for all the given EPeople private static void createTasks(Context c, WorkflowItem wi, EPerson[] epa) throws SQLException { // create a tasklist entry for each eperson for (int i = 0; i < epa.length; i++) { // can we get away without creating a tasklistitem class? // do we want to? TableRow tr = DatabaseManager.create(c, "tasklistitem"); tr.setColumn("eperson_id", epa[i].getID()); tr.setColumn("workflow_id", wi.getID()); DatabaseManager.update(c, tr); } }
@Override public void link(Collection collection, Item item) throws AuthorizeException { if (!linked(collection, item)) { try { TableRow row = DatabaseManager.create(context, "collection2item"); row.setColumn("collection_id", collection.getID()); row.setColumn("item_id", item.getID()); DatabaseManager.update(context, row); } catch (SQLException sqle) { throw new RuntimeException(sqle); } } }
/** Record that this application is running. */ public void register() { // Create the database entry Timestamp now = new Timestamp(started.getTime()); try { Context context = new Context(); row = DatabaseManager.create(context, "Webapp"); row.setColumn("AppName", kind); row.setColumn("URL", url); row.setColumn("Started", now); row.setColumn("isUI", isUI() ? 1 : 0); // update won't widen boolean to integer DatabaseManager.update(context, row); context.complete(); } catch (SQLException e) { log.error("Failed to record startup in Webapp table.", e); } }
private void populateTableRowFromItem(Item item, TableRow row) { EPerson submitter = item.getSubmitter(); Collection owningCollection = item.getOwningCollection(); row.setColumn("item_id", item.getID()); row.setColumn("in_archive", item.isArchived()); row.setColumn("withdrawn", item.isWithdrawn()); row.setColumn("last_modified", item.getLastModified()); if (submitter != null) { row.setColumn("submitter_id", submitter.getID()); } if (owningCollection != null) { row.setColumn("owning_collection", owningCollection.getID()); } }
@Override public void update(Item item) throws AuthorizeException { try { TableRow row = DatabaseManager.find(context, "item", item.getID()); if (row != null) { // Fill out the TableRow and save it populateTableRowFromItem(item, row); row.setColumn("last_modified", new Date()); row.setColumn("uuid", item.getIdentifier().getUUID().toString()); DatabaseManager.update(context, row); } else { throw new RuntimeException("Didn't find item " + item.getID()); } } catch (SQLException sqle) { throw new RuntimeException(sqle); } }
/** * set owner of WorkflowItem * * @param ep owner */ public void setOwner(EPerson ep) { owner = ep; if (ep == null) { wfRow.setColumnNull("owner"); } else { wfRow.setColumn("owner", ep.getID()); } }
/** * Insert a table row into the RDBMS. * * @param context Current DSpace context * @param row The row to insert * @exception SQLException If a database error occurs */ public static void insert(Context context, TableRow row) throws SQLException { int newID; if (isPostgres) { newID = doInsertPostgres(context, row); } else { newID = doInsertGeneric(context, row); } row.setColumn(getPrimaryKeyColumn(row), newID); }
@Override public void link(Item item, Bundle bundle) throws AuthorizeException { if (linked(item, bundle)) { return; } try { TableRow row = DatabaseManager.create(context, "item2bundle"); row.setColumn("item_id", item.getID()); row.setColumn("bundle_id", bundle.getID()); DatabaseManager.update(context, row); // If we're adding the Bundle to the Item, we bequeath our // policies unto it. AuthorizeManager.inheritPolicies(context, item, bundle); } catch (SQLException sqle) { throw new RuntimeException(sqle); } }
/** * startWorkflow() begins a workflow - in a single transaction do away with the PersonalWorkspace * entry and turn it into a WorkflowItem. * * @param c Context * @param wsi The WorkspaceItem to convert to a workflow item * @return The resulting workflow item */ public static WorkflowItem start(Context c, WorkspaceItem wsi) throws SQLException, AuthorizeException, IOException { // FIXME Check auth Item myitem = wsi.getItem(); Collection collection = wsi.getCollection(); log.info( LogManager.getHeader( c, "start_workflow", "workspace_item_id=" + wsi.getID() + "item_id=" + myitem.getID() + "collection_id=" + collection.getID())); // record the start of the workflow w/provenance message recordStart(c, myitem); // create the WorkflowItem TableRow row = DatabaseManager.create(c, "workflowitem"); row.setColumn("item_id", myitem.getID()); row.setColumn("collection_id", wsi.getCollection().getID()); WorkflowItem wfi = new WorkflowItem(c, row); wfi.setMultipleFiles(wsi.hasMultipleFiles()); wfi.setMultipleTitles(wsi.hasMultipleTitles()); wfi.setPublishedBefore(wsi.isPublishedBefore()); // remove the WorkspaceItem wsi.deleteWrapper(); // now get the worflow started doState(c, wfi, WFSTATE_STEP1POOL, null); // Return the workflow item return wfi; }
/** * Update the metadata field in the database. * * @param context dspace context * @throws SQLException * @throws AuthorizeException * @throws NonUniqueMetadataException * @throws IOException */ public void update(Context context) throws SQLException, AuthorizeException, NonUniqueMetadataException, IOException { // Check authorisation: Only admins may update the metadata registry if (!AuthorizeManager.isAdmin(context)) { throw new AuthorizeException("Only administrators may modiffy the Dublin Core registry"); } // Check to see if the schema ID was altered. If is was then we will // query to ensure that there is not already a duplicate name field. if (row.getIntColumn("metadata_schema_id") != schemaID) { if (MetadataField.hasElement(context, schemaID, element, qualifier)) { throw new NonUniqueMetadataException("Duplcate field name found in target schema"); } } // Ensure the element and qualifier are unique within a given schema. if (!unique(context, schemaID, element, qualifier)) { throw new NonUniqueMetadataException("Please make " + element + "." + qualifier); } row.setColumn("metadata_schema_id", schemaID); row.setColumn("element", element); row.setColumn("qualifier", qualifier); row.setColumn("scope_note", scopeNote); DatabaseManager.update(context, row); decache(); log.info( LogManager.getHeader( context, "update_metadatafieldregistry", "metadata_field_id=" + getFieldID() + "element=" + getElement() + "qualifier=" + getQualifier())); }
public void create(Context context) throws SQLException, AuthorizeException { // Create a table row and update it with the values row = DatabaseManager.row("revision_token"); row.setColumn("revision_token_id", revisionTokenId); row.setColumn("email", email); row.setColumn("tipo", tipo); row.setColumn("token", token); row.setColumn("handle_revisado", handleRevisado); row.setColumn("workspace_id", workspaceId); row.setColumn("revision_id", revisionId); DatabaseManager.insert(context, row); }
@Override public EPerson create() throws AuthorizeException { UUID uuid = UUID.randomUUID(); try { TableRow row = DatabaseManager.create(context, "eperson"); row.setColumn("uuid", uuid.toString()); DatabaseManager.update(context, row); int id = row.getIntColumn("eperson_id"); EPerson eperson = new EPerson(context, id); eperson.setIdentifier(new ObjectIdentifier(uuid)); return eperson; } catch (SQLException sqle) { throw new RuntimeException(sqle); } }
/** * Update the metadata value in the database. * * @param context dspace context * @throws SQLException * @throws AuthorizeException */ public void update(Context context) throws SQLException, AuthorizeException { row.setColumn("revision_token_id", revisionTokenId); row.setColumn("email", email); row.setColumn("tipo", tipo); row.setColumn("token", token); row.setColumn("handle_revisado", handleRevisado); if (workspaceId == null) { row.setColumnNull("workspace_id"); } else { row.setColumn("workspace_id", workspaceId); } if (revisionId == null) { row.setColumnNull("revision_id"); } else { row.setColumn("revision_id", revisionId); } DatabaseManager.update(context, row); }
private void populateTableRowFromCollection(Collection collection, TableRow row) { int id = collection.getID(); Bitstream logo = collection.getLogo(); Item templateItem = collection.getTemplateItem(); Group admins = collection.getAdministrators(); Group[] workflowGroups = collection.getWorkflowGroups(); if (logo == null) { row.setColumnNull("logo_bitstream_id"); } else { row.setColumn("logo_bitstream_id", logo.getID()); } if (templateItem == null) { row.setColumnNull("template_item_id"); } else { row.setColumn("template_item_id", templateItem.getID()); } if (admins == null) { row.setColumnNull("admin"); } else { row.setColumn("admin", admins.getID()); } for (int i = 1; i <= workflowGroups.length; i++) { Group g = workflowGroups[i - 1]; if (g == null) { row.setColumnNull("workflow_step_" + i); } else { row.setColumn("workflow_step_" + i, g.getID()); } } // Now loop over all allowed metadata fields and set the value into the // TableRow. for (CollectionMetadataField field : CollectionMetadataField.values()) { String value = collection.getMetadata(field.toString()); if (value == null) { row.setColumnNull(field.toString()); } else { row.setColumn(field.toString(), value); } } row.setColumn("uuid", collection.getIdentifier().getUUID().toString()); }
public void setMultipleTitles(boolean b) { wfRow.setColumn("multiple_titles", b); }
/** * set name of group * * @param name new group name */ public void setName(String name) { myRow.setColumn("name", name); modifiedMetadata = true; addDetails("name"); }
/** * Regenerate the group cache AKA the group2groupcache table in the database - meant to be called * when a group is added or removed from another group */ private void rethinkGroupCache() throws SQLException { // read in the group2group table TableRowIterator tri = DatabaseManager.queryTable(myContext, "group2group", "SELECT * FROM group2group"); Map<Integer, Set<Integer>> parents = new HashMap<Integer, Set<Integer>>(); try { while (tri.hasNext()) { TableRow row = (TableRow) tri.next(); Integer parentID = Integer.valueOf(row.getIntColumn("parent_id")); Integer childID = Integer.valueOf(row.getIntColumn("child_id")); // if parent doesn't have an entry, create one if (!parents.containsKey(parentID)) { Set<Integer> children = new HashSet<Integer>(); // add child id to the list children.add(childID); parents.put(parentID, children); } else { // parent has an entry, now add the child to the parent's record // of children Set<Integer> children = parents.get(parentID); children.add(childID); } } } finally { // close the TableRowIterator to free up resources if (tri != null) { tri.close(); } } // now parents is a hash of all of the IDs of groups that are parents // and each hash entry is a hash of all of the IDs of children of those // parent groups // so now to establish all parent,child relationships we can iterate // through the parents hash for (Map.Entry<Integer, Set<Integer>> parent : parents.entrySet()) { Set<Integer> myChildren = getChildren(parents, parent.getKey()); parent.getValue().addAll(myChildren); } // empty out group2groupcache table DatabaseManager.updateQuery(myContext, "DELETE FROM group2groupcache WHERE id >= 0"); // write out new one for (Map.Entry<Integer, Set<Integer>> parent : parents.entrySet()) { int parentID = parent.getKey().intValue(); for (Integer child : parent.getValue()) { TableRow row = DatabaseManager.row("group2groupcache"); row.setColumn("parent_id", parentID); row.setColumn("child_id", child); DatabaseManager.insert(myContext, row); } } }
/** * Generic version of row insertion with separate id get / insert * * @param context * @param row * @return * @throws SQLException */ private static int doInsertGeneric(Context context, TableRow row) throws SQLException { int newID = -1; String table = row.getTable(); PreparedStatement statement = null; ResultSet rs = null; try { // Get an ID (primary key) for this row by using the "getnextid" // SQL function in Postgres, or directly with sequences in Oracle if (isOracle) { statement = context .getDBConnection() .prepareStatement("SELECT " + table + "_seq" + ".nextval FROM dual"); } else { statement = context.getDBConnection().prepareStatement("SELECT getnextid(?) AS result"); loadParameters(statement, new Object[] {table}); } rs = statement.executeQuery(); rs.next(); newID = rs.getInt(1); } finally { if (rs != null) { try { rs.close(); } catch (SQLException sqle) { } } if (statement != null) { try { statement.close(); } catch (SQLException sqle) { } } } if (newID < 0) { throw new SQLException("Unable to retrieve sequence ID"); } // Set the ID in the table row object row.setColumn(getPrimaryKeyColumn(table), newID); Collection<ColumnInfo> info = getColumnInfo(table); String sql = insertSQL.get(table); if (sql == null) { StringBuilder sqlBuilder = new StringBuilder().append("INSERT INTO ").append(table).append(" ( "); boolean firstColumn = true; for (ColumnInfo col : info) { if (firstColumn) { sqlBuilder.append(col.getName()); firstColumn = false; } else { sqlBuilder.append(",").append(col.getName()); } } sqlBuilder.append(") VALUES ( "); // Values to insert firstColumn = true; for (int i = 0; i < info.size(); i++) { if (firstColumn) { sqlBuilder.append("?"); firstColumn = false; } else { sqlBuilder.append(",").append("?"); } } // Watch the syntax sqlBuilder.append(")"); sql = sqlBuilder.toString(); insertSQL.put(table, sql); } execute(context.getDBConnection(), sql, info, row); return newID; }
public void setMultipleFiles(boolean b) { wfRow.setColumn("multiple_files", b); }
public void setRoleId(String id) { myRow.setColumn("role_id", id); }
public void setCollectionId(int id) { myRow.setColumn("collection_id", id); }
/** * Set state of WorkflowItem * * @param newstate new state (from <code>WorkflowManager</code>) */ public void setState(int newstate) { wfRow.setColumn("state", newstate); }
/** * Convert the current row in a ResultSet into a TableRow object. * * @param results A ResultSet to process * @param table The name of the table * @param pColumnNames The name of the columns in this resultset * @return A TableRow object with the data from the ResultSet * @exception SQLException If a database error occurs */ static TableRow process(ResultSet results, String table, List<String> pColumnNames) throws SQLException { ResultSetMetaData meta = results.getMetaData(); int columns = meta.getColumnCount() + 1; // If we haven't been passed the column names try to generate them from the metadata / table List<String> columnNames = pColumnNames != null ? pColumnNames : ((table == null) ? getColumnNames(meta) : getColumnNames(table)); TableRow row = new TableRow(canonicalize(table), columnNames); // Process the columns in order // (This ensures maximum backwards compatibility with // old JDBC drivers) for (int i = 1; i < columns; i++) { String name = meta.getColumnName(i); int jdbctype = meta.getColumnType(i); switch (jdbctype) { case Types.BOOLEAN: case Types.BIT: row.setColumn(name, results.getBoolean(i)); break; case Types.INTEGER: if (isOracle) { long longValue = results.getLong(i); if (longValue <= (long) Integer.MAX_VALUE) { row.setColumn(name, (int) longValue); } else { row.setColumn(name, longValue); } } else { row.setColumn(name, results.getInt(i)); } break; case Types.BIGINT: row.setColumn(name, results.getLong(i)); break; case Types.NUMERIC: case Types.DECIMAL: row.setColumn(name, results.getBigDecimal(i)); break; case Types.DOUBLE: row.setColumn(name, results.getDouble(i)); break; case Types.CLOB: if (isOracle) { row.setColumn(name, results.getString(i)); } else { throw new IllegalArgumentException("Unsupported JDBC type: " + jdbctype); } break; case Types.VARCHAR: try { byte[] bytes = results.getBytes(i); if (bytes != null) { String mystring = new String(results.getBytes(i), "UTF-8"); row.setColumn(name, mystring); } else { row.setColumn(name, results.getString(i)); } } catch (UnsupportedEncodingException e) { log.error("Unable to parse text from database", e); } break; case Types.DATE: row.setColumn(name, results.getDate(i)); break; case Types.TIME: row.setColumn(name, results.getTime(i)); break; case Types.TIMESTAMP: row.setColumn(name, results.getTimestamp(i)); break; default: throw new IllegalArgumentException("Unsupported JDBC type: " + jdbctype); } // Determines if the last column was null, and sets the tablerow accordingly if (results.wasNull()) { row.setColumnNull(name); } } // Now that we've prepped the TableRow, reset the flags so that we can detect which columns have // changed row.resetChanged(); return row; }
public void setPublishedBefore(boolean b) { wfRow.setColumn("published_before", b); }
public void setGroupId(Group group) { myRow.setColumn("group_id", group.getID()); }