Project

General

Profile

« Previous | Next » 

Revision 5895

Added by berkley about 13 years ago

added code to do database query for listObjects

View differences:

IdentifierManager.java
29 29
import java.sql.PreparedStatement;
30 30
import java.sql.ResultSet;
31 31
import java.sql.SQLException;
32
import java.sql.Timestamp;
32 33

  
33 34
import org.apache.log4j.Logger;
35
import org.dataone.service.types.Checksum;
36
import org.dataone.service.types.ChecksumAlgorithm;
37
import org.dataone.service.types.ObjectFormat;
38
import org.dataone.service.types.ObjectInfo;
39
import org.dataone.service.types.ObjectList;
34 40
import org.dataone.service.types.SystemMetadata;
41
import org.dataone.service.types.Identifier;
35 42

  
36 43
import edu.ucsb.nceas.metacat.database.DBConnection;
37 44
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
38 45
import edu.ucsb.nceas.metacat.properties.PropertyService;
46
import edu.ucsb.nceas.metacat.shared.ServiceException;
39 47
import edu.ucsb.nceas.metacat.util.DocumentUtil;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
40 49

  
41 50
/**
42 51
 * Manage the relationship between Metacat local identifiers (LocalIDs) that are
......
92 101
    }
93 102
    
94 103
    /**
104
     * return a hash of all of the info that is in the systemmetadata table
105
     * @param localId
106
     * @return
107
     */
108
    public Hashtable<String, String> getSystemMetadataInfo(String localId)
109
    throws McdbDocNotFoundException
110
    {
111
        try
112
        {
113
            AccessionNumber acc = new AccessionNumber(localId, "NONE");
114
            localId = acc.getDocid();
115
        }
116
        catch(Exception e)
117
        {
118
            //do nothing. just try the localId as it is
119
        }
120
        Hashtable<String, String> h = new Hashtable<String, String>();
121
        String sql = "select guid, date_uploaded, rights_holder, checksum, checksum_algorithm, " +
122
          "origin_member_node, authoritative_member_node, date_modified, submitter " +
123
          "from systemmetadata where docid = ?";
124
        DBConnection dbConn = null;
125
        int serialNumber = -1;
126
        try 
127
        {
128
            // Get a database connection from the pool
129
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getDocumentInfo");
130
            serialNumber = dbConn.getCheckOutSerialNumber();
131

  
132
            // Execute the insert statement
133
            PreparedStatement stmt = dbConn.prepareStatement(sql);
134
            stmt.setString(1, localId);
135
            ResultSet rs = stmt.executeQuery();
136
            if (rs.next()) 
137
            {
138
                String guid = rs.getString(1);
139
                Timestamp dateUploaded = rs.getTimestamp(2);
140
                String rightsHolder = rs.getString(3);
141
                String checksum = rs.getString(4);
142
                String checksumAlgorithm = rs.getString(5);
143
                String originMemberNode = rs.getString(6);
144
                String authoritativeMemberNode = rs.getString(7);
145
                Timestamp dateModified = rs.getTimestamp(8);
146
                String submitter = rs.getString(9);
147
                
148
                h.put("guid", guid);
149
                h.put("date_uploaded", new Long(dateUploaded.getTime()).toString());
150
                h.put("rights_holder", rightsHolder);
151
                h.put("checksum", checksum);
152
                h.put("checksum_algorithm", checksumAlgorithm);
153
                h.put("origin_member_node", originMemberNode);
154
                h.put("authoritative_member_node", authoritativeMemberNode);
155
                h.put("date_modified", new Long(dateModified.getTime()).toString());
156
                h.put("submitter", submitter);
157
                
158
                stmt.close();
159
            } 
160
            else
161
            {
162
                stmt.close();
163
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
164
                throw new McdbDocNotFoundException("2Could not find document " + localId);
165
            }
166
            
167
        } 
168
        catch (SQLException e) 
169
        {
170
            e.printStackTrace();
171
            logMetacat.error("Error while getting system metadata info for localid " + localId + " : "  
172
                    + e.getMessage());
173
        } 
174
        finally 
175
        {
176
            // Return database connection to the pool
177
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
178
        }
179
        return h;
180
    }
181
    
182
    /**
95 183
     * return information on the document with localId.  These are the fields
96 184
     * from the xml_documents table.  They can be used to contstruct metadata 
97 185
     * about the object that is stored.
......
708 796
    }
709 797
    
710 798
    /**
799
     * query the systemmetadata table based on the given parameters
800
     * @param startTime
801
     * @param endTime
802
     * @param objectFormat
803
     * @param replicaStatus
804
     * @param start
805
     * @param count
806
     * @return ObjectList
807
     */
808
    public ObjectList querySystemMetadata(Date startTime, Date endTime, 
809
            ObjectFormat objectFormat, boolean replicaStatus, int start, int count)
810
    {
811
        ObjectList ol = new ObjectList();
812
        int total = 0;
813
        
814
        try
815
        {
816
            String sql = "select guid, date_uploaded, rights_holder, checksum, " +
817
                "checksum_algorithm, origin_member_node, authoritive_member_node, " +
818
                "date_modified, submitter, xml_documents.doctype from systemmetadata, " +
819
                "xml_documents where xml_documents.docid in " +
820
                "(select docid from identifier where guid = systemmetadata.guid)";
821
            
822
            boolean f1 = false;
823
            boolean f2 = false;
824
            boolean f3 = false;
825
            
826
            if(startTime != null)
827
            {
828
                sql += " and systemmetadata.date_modified > ?";
829
                f1 = true;
830
            }
831
            
832
            if(endTime != null)
833
            {
834
                sql += " and systemmetadata.date_modified < ?";
835
                f2 = true;
836
            }
837
            
838
            if(objectFormat != null)
839
            {
840
                sql += " and xml_documents.doctype = ?";
841
                f3 = true;
842
            }
843
            
844
            if(replicaStatus)
845
            {
846
                String currentNodeId = PropertyService.getInstance().getProperty("dataone.memberNodeId");
847
                sql += " and authoritive_member_node != '" + currentNodeId.trim() + "'";
848
            }
849
            
850
            DBConnection dbConn = DBConnectionPool.getDBConnection("IdentifierManager.createMapping");
851
            int serialNumber = dbConn.getCheckOutSerialNumber();
852
            PreparedStatement stmt = dbConn.prepareStatement(sql);
853
            
854
            if(f1 && f2 && f3)
855
            {
856
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
857
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
858
                stmt.setString(3, objectFormat.toString());
859
            }
860
            else if(f1 && f2 && !f3)
861
            {
862
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
863
                stmt.setTimestamp(2, new Timestamp(endTime.getTime()));
864
            }
865
            else if(f1 && !f2 && f3)
866
            {
867
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
868
                stmt.setString(2, objectFormat.toString());
869
            }
870
            else if(f1 && !f2 && !f3)
871
            {
872
                stmt.setTimestamp(1, new Timestamp(startTime.getTime()));
873
            }
874
            else if(!f1 && f2 && f3)
875
            {
876
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
877
                stmt.setString(2, objectFormat.toString());
878
            }
879
            else if(!f1 && !f2 && f3)
880
            {
881
                stmt.setString(1, objectFormat.toString());
882
            }
883
            else if(!f1 && f2 && !f3)
884
            {
885
                stmt.setTimestamp(1, new Timestamp(endTime.getTime()));
886
            }
887
            
888
            //System.out.println("LISTOBJECTS QUERY: " + stmt.toString());
889
            
890
            ResultSet rs = stmt.executeQuery();
891
            for(int i=0; i<start - 1; i++)
892
            {
893
                rs.next();
894
            }
895
            int countIndex = 0;
896
                        
897
            while(rs.next()) 
898
            {
899
                total++;
900
                if(countIndex >= count)
901
                {
902
                    break;
903
                }
904
                String guid = rs.getString(1);
905
                //System.out.println("query found doc with guid " + guid);
906
                //Timestamp dateUploaded = rs.getTimestamp(2);
907
                //String rightsHolder = rs.getString(3);
908
                String checksum = rs.getString(4);
909
                String checksumAlgorithm = rs.getString(5);
910
                //String originMemberNode = rs.getString(6);
911
                String authoritiveMemberNode = rs.getString(7);
912
                Timestamp dateModified = rs.getTimestamp(8);
913
                //String submitter = rs.getString(9);
914
                String format = rs.getString(10);
915
                
916
                ObjectInfo oi = new ObjectInfo();
917
                
918
                Identifier id = new Identifier();
919
                id.setValue(guid);
920
                oi.setIdentifier(id);
921
                
922
                oi.setDateSysMetadataModified(new Date(dateModified.getTime()));
923
                
924
                Checksum cs = new Checksum();
925
                cs.setValue(checksum);
926
                cs.setAlgorithm(ChecksumAlgorithm.convert(checksumAlgorithm));
927
                oi.setChecksum(cs);
928
                
929
                ObjectFormat oFormat = ObjectFormat.convert(format);
930
                if(oFormat != null)
931
                {
932
                    oi.setObjectFormat(oFormat);
933
                }
934
                
935
                ol.addObjectInfo(oi);
936
                countIndex++;
937
            }
938
            
939
            while(rs.next())
940
            { //expend the resultset to get the total count of possible rows
941
                total++;
942
            }
943
        }
944
        catch(Exception e)
945
        {
946
           e.printStackTrace();
947
           System.out.println("Error querying system metadata: " + e.getMessage());
948
        }
949
        
950
        System.out.println("listObjects total: " + total);
951
        ol.setStart(start);
952
        ol.setCount(count);
953
        ol.setTotal(total);
954
        
955
        return ol;
956
    }
957
    
958
    /**
711 959
     * create a mapping in the systemmetadata or identifier table
712 960
     * @param guid
713 961
     * @param localId

Also available in: Unified diff