Changeset 1034

Show
Ignore:
Timestamp:
06/19/09 15:47:06 (4 years ago)
Author:
goldfd
Message:

Merged revision 1033 into stage

Location:
branches/stage/mpdb-server/src/edu/rpi/metpetdb/server
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/stage/mpdb-server/src/edu/rpi/metpetdb/server/ExcelServlet.java

    r957 r1034  
    6060                        response.getWriter().write("\n"); 
    6161                        for (Sample s : samples){ 
    62                                 response.getWriter().write(s.getNumber() + "\t"); 
    63                                 response.getWriter().write(s.isPublicData() + "\t"); 
     62                                response.getWriter().write(nullToEmptyString(s.getNumber()) + "\t"); 
     63                                response.getWriter().write(boolToString(s.isPublicData()) + "\t"); 
    6464                                response.getWriter().write(s.getSubsampleCount() + "\t"); 
    6565                                response.getWriter().write(s.getImageCount() + "\t"); 
     
    6767                                response.getWriter().write(s.getOwner().getName() + "\t"); 
    6868                                response.getWriter().write(setToString(s.getRegions(),RegionProperty.name) + "\t");                      
    69                                 response.getWriter().write(s.getCountry() + "\t"); 
    70                                 response.getWriter().write(s.getRockType() + "\t"); 
     69                                response.getWriter().write(nullToEmptyString(s.getCountry()) + "\t"); 
     70                                response.getWriter().write(nullToEmptyString(s.getRockType()) + "\t"); 
    7171                                response.getWriter().write(setToString(s.getMetamorphicGrades(),MetamorphicGradeProperty.name) + "\t"); 
    7272                                response.getWriter().write(setSampleMineralsToString(s.getMinerals()) + "\t"); 
     
    7474                                response.getWriter().write(formatlatlng(((Point)s.getLocation()).y) +"\t"); 
    7575                                response.getWriter().write(formatlatlng(((Point)s.getLocation()).x) +"\t"); 
    76                                 response.getWriter().write(s.getSesarNumber() + "\t"); 
    77                                 response.getWriter().write(s.getCollector() + "\t"); 
     76                                response.getWriter().write(nullToEmptyString(s.getSesarNumber()) + "\t"); 
     77                                response.getWriter().write(nullToEmptyString(s.getCollector()) + "\t"); 
    7878                                response.getWriter().write(Sample.dateToString(s.getCollectionDate(), s.getDatePrecision()) + "\t"); 
    79                                 response.getWriter().write(s.getLocationText() + "\t"); 
     79                                response.getWriter().write(nullToEmptyString(s.getLocationText()) + "\t"); 
    8080                                response.getWriter().write("\n"); 
    8181                        } 
     
    8585                 
    8686                return; 
     87        } 
     88         
     89        private String boolToString(final Boolean b){ 
     90                return (b) ? "yes" : "no"; 
     91        } 
     92         
     93        private String nullToEmptyString(final Object o){ 
     94                return (o == null) ? "" : o.toString(); 
    8795        } 
    8896 
     
    101109                String text = ""; 
    102110                for (SampleMineral sm : minerals){ 
    103                         text += sm.getName() + " (" + sm.getAmount()+ "), "; 
     111                        text += sm.toString() + ", ";            
    104112                } 
    105113                if (!text.equals("")){ 
  • branches/stage/mpdb-server/src/edu/rpi/metpetdb/server/search/SearchDb.java

    r1032 r1034  
    8181                        // Get the subsample ids that correspond to the chemical analyses. 
    8282                        System.out.println("Search Query Project Subsample Ids:" + chemQuery.getQueryString()); 
    83                         final List<Long> projectedSubsampleIds = chemQuery.list(); 
     83 
    8484                        // no chemical analysis fit search, no reason to try finding samples with "no matches" 
    85                         if (projectedSubsampleIds.size() == 0) { 
     85                        String  subsampleIds = getLongIdStringForList(chemQuery.list()); 
     86                        if (subsampleIds == null) { 
    8687                                return new Results<Sample>(0, new ArrayList<Sample>()); 
    8788                        } 
    88                         String subsampleIds = ""; 
    89                         for (Long id : projectedSubsampleIds){ 
    90                                 subsampleIds += ((Long)id).toString() + ","; 
    91                         }        
    92                         if (subsampleIds != ""){ 
    93                                 subsampleIds = subsampleIds.substring(0, subsampleIds.length()-1); 
    94                         } 
     89 
    9590                        // Get samples third... will need to incorporate top two pieces 
    9691                        fullQuery = getSamplesQuery(searchSamp, userSearching, session); 
    9792                        FullTextQuery sampleQuery = fullTextSession.createFullTextQuery(fullQuery, Sample.class); 
    9893                        System.out.println("Search Query Project Sample Ids:" + fullQuery.toString()); 
    99                         final List<Object[]> projectedSampleIds = sampleQuery.setProjection("id").list(); 
    100                         if (projectedSampleIds.size() == 0) { 
     94                        String  sampleIds = getLongIdString(sampleQuery.setProjection("id").list()); 
     95                        if (sampleIds == null) { 
    10196                                return new Results<Sample>(0, new ArrayList<Sample>()); 
    10297                        } 
    103                         String  sampleIds = ""; 
    104                         for (Object[] o : projectedSampleIds){ 
    105                                 for (Object id : o){ 
    106                                         sampleIds += ((Long)id).toString() + ","; 
    107                                 } 
    108                         } 
    109                         if (sampleIds != ""){ 
    110                                 sampleIds = sampleIds.substring(0, sampleIds.length()-1); 
    111                         } 
     98 
    11299                        org.hibernate.Query hql = session.createQuery("from Sample s where s.id in (" + sampleIds + 
    113100                                        ") and exists (select 1 from Subsample ss where ss.sampleId = s.id and ss.id in (" +  
     
    115102                        org.hibernate.Query sizeQuery = session.createQuery("select count(*) " + hql.getQueryString()); 
    116103                        System.out.println("Search Query Find Samples by Id and SubsampleId:" + hql.getQueryString()); 
    117                         if (p != null) { 
    118                                 hql.setFirstResult(p.getFirstResult()); 
    119                                 hql.setMaxResults(p.getMaxResults()); 
    120                         } else { 
    121                                 hql.setFirstResult(0); 
    122                                 hql.setMaxResults(100); 
    123                         } 
     104                        hql = setPagination(p,hql); 
    124105                        try { 
    125106                                //this is here in order to convert java.util.Collection$EmptyList into 
     
    137118                System.out.println("Search Query:" + fullQuery.toString()); 
    138119 
    139                 final FullTextQuery hibQuery = fullTextSession.createFullTextQuery( 
    140                                 fullQuery, Sample.class); 
    141  
    142                 if (p != null) { 
    143                         hibQuery.setFirstResult(p.getFirstResult()); 
    144                         hibQuery.setMaxResults(p.getMaxResults()); 
    145                 } else { 
    146                         //place an upper limit of 100 search results 
    147                         hibQuery.setFirstResult(0); 
    148                         hibQuery.setMaxResults(100); 
    149                 } 
    150                 try { 
    151                         //this is here in order to convert java.util.Collection$EmptyList into 
    152                         //the correct representation so it can be serialized by GWT 
    153                         List<Sample> list = hibQuery.list(); 
    154                         if (list.size() == 0) 
    155                                 list = new ArrayList<Sample>(); 
    156                         int resultSize = 0; 
     120                try {                    
    157121                        if (session.getEnabledFilter("boundingBox")!=null){ 
    158122                                FullTextQuery sizeHibQuery = fullTextSession.createFullTextQuery( 
    159123                                                fullQuery, Sample.class); 
    160                                 final List<Object[]> projectedSampleIds = sizeHibQuery.setProjection("id").list(); 
    161                                 String  sampleIds = ""; 
    162                                 for (Object[] o : projectedSampleIds){ 
    163                                         for (Object id : o){ 
    164                                                 sampleIds += ((Long)id).toString() + ","; 
    165                                         } 
     124                                String  sampleIds = getLongIdString(sizeHibQuery.setProjection("id").list()); 
     125                                if (sampleIds == null) { 
     126                                        return new Results<Sample>(0, new ArrayList<Sample>()); 
    166127                                } 
    167                                 if (sampleIds != ""){ 
    168                                         sampleIds = sampleIds.substring(0, sampleIds.length()-1); 
    169                                 } 
    170                                 org.hibernate.Query sizeQuery = session.createQuery("select count(*) from Sample s where s.id in (" + sampleIds + ")"); 
    171                                 resultSize = ((Long)sizeQuery.uniqueResult()).intValue(); 
     128                                org.hibernate.Query resultQuery = session.createQuery("from Sample s where s.id in (" + sampleIds + ")"); 
     129                                org.hibernate.Query sizeQuery = session.createQuery("select count(*) " + resultQuery.getQueryString()); 
     130                                resultQuery = setPagination(p,resultQuery); 
     131                                List<Sample> list = resultQuery.list(); 
     132                                if (list.size() == 0) 
     133                                        list = new ArrayList<Sample>(); 
     134                                final Results<Sample> results = new Results<Sample>(((Long)sizeQuery.uniqueResult()).intValue(),list); 
     135                                return results; 
    172136                        } else { 
    173                                 resultSize = hibQuery.getResultSize(); 
    174                         } 
    175                         final Results<Sample> results = new Results<Sample>(resultSize, list); 
    176                         return results; 
     137                                FullTextQuery hibQuery = fullTextSession.createFullTextQuery( 
     138                                                fullQuery, Sample.class); 
     139                                hibQuery = setPagination(p,hibQuery); 
     140                                //this is here in order to convert java.util.Collection$EmptyList into 
     141                                //the correct representation so it can be serialized by GWT 
     142                                List<Sample> list = hibQuery.list(); 
     143                                if (list.size() == 0) 
     144                                        list = new ArrayList<Sample>(); 
     145                                final Results<Sample> results = new Results<Sample>(hibQuery.getResultSize(), list); 
     146                                return results; 
     147                        } 
    177148                } catch (CallbackException e) { 
    178149                        throw ConvertSecurityException.convertToException(e); 
     
    201172                        System.out.println("Search Query:" + fullQuery.toString());                      
    202173 
    203                         if (p != null) { 
    204                                 fullQuery.setFirstResult(p.getFirstResult()); 
    205                                 fullQuery.setMaxResults(p.getMaxResults()); 
    206                         } else { 
    207                                 //place an upper limit of 100 search results 
    208                                 fullQuery.setFirstResult(0); 
    209                                 fullQuery.setMaxResults(100); 
    210                         } 
     174                        fullQuery = setPagination(p,fullQuery); 
    211175                        try { 
    212176                                //this is here in order to convert java.util.Collection$EmptyList into 
     
    229193                                        .createFullTextQuery(tempSampleQuery, Subsample.class); 
    230194                        System.out.println("Search Query Project Subsample Ids:" + sampleQuery.toString());      
    231                         final List<Object[]> projectedSubsampleIds = sampleQuery.setProjection("id").list(); 
    232                         // no samples fit search, no reason to try finding chemical analyses with "no matches" 
    233                         if (projectedSubsampleIds.size() == 0) { 
     195                        String  subsampleIds = getLongIdString(sampleQuery.setProjection("id").list()); 
     196                        if (subsampleIds == null) { 
    234197                                return new Results<ChemicalAnalysis>(0, new ArrayList<ChemicalAnalysis>()); 
    235                         } 
    236                         String subsampleIds = ""; 
    237                         for (Object[] o : projectedSubsampleIds){ 
    238                                 for (Object id : o){ 
    239                                         subsampleIds += ((Long)id).toString() + ","; 
    240                                 } 
    241                         }        
    242                         if (subsampleIds != ""){ 
    243                                 subsampleIds = subsampleIds.substring(0, subsampleIds.length()-1); 
    244198                        } 
    245199                        // Get chemical analyses third... will need to incorporate top two pieces 
    246200                        org.hibernate.Query chemQuery = getChemicalsQuery(searchSamp, userSearching, session, RETURN_CHEMICAL_ANALYSIS_ID); 
    247201                        System.out.println("Search Query Project Chemical Analysis Ids:" + chemQuery.getQueryString());  
    248                         final List<Integer> projectedChemIds = chemQuery.list(); 
    249                         if (projectedChemIds.size() == 0){ 
     202 
     203                        String  chemIds = getIntIdStringForList(chemQuery.list()); 
     204                        if (chemIds == null) { 
    250205                                return new Results<ChemicalAnalysis>(0, new ArrayList<ChemicalAnalysis>()); 
    251                         } 
    252                         String  chemIds = ""; 
    253                         for (Integer id : projectedChemIds){ 
    254                                 chemIds += ((Integer)id).toString() + ","; 
    255                         } 
    256                         if (chemIds != ""){ 
    257                                 chemIds = chemIds.substring(0, chemIds.length()-1); 
    258206                        } 
    259207                        org.hibernate.Query hql = session.createQuery("from ChemicalAnalysis ca where ca.id in (" + chemIds + 
     
    262210                        org.hibernate.Query sizeQuery = session.createQuery("select count(*) " + hql.getQueryString()); 
    263211                        System.out.println("Search Query Find Chemical Analyses by Id and SubsampleId:" + hql.getQueryString()); 
    264                          
    265                         if (p != null) { 
    266                                 hql.setFirstResult(p.getFirstResult()); 
    267                                 hql.setMaxResults(p.getMaxResults()); 
    268                         } else { 
    269                                 hql.setFirstResult(0); 
    270                                 hql.setMaxResults(100); 
    271                         } 
     212 
     213                        hql = setPagination(p,hql); 
    272214                        try { 
    273215                                //this is here in order to convert java.util.Collection$EmptyList into 
     
    680622                return false; 
    681623        } 
     624         
     625        private static org.hibernate.Query setPagination(PaginationParameters p, org.hibernate.Query q){ 
     626                if (p != null){ 
     627                        q.setFirstResult(p.getFirstResult()); 
     628                        q.setMaxResults(p.getMaxResults()); 
     629                } else { 
     630                        q.setFirstResult(0); 
     631                        q.setMaxResults(100); 
     632                } 
     633                return q; 
     634        } 
     635         
     636        private static FullTextQuery setPagination(PaginationParameters p, FullTextQuery q){ 
     637                if (p != null){ 
     638                        q.setFirstResult(p.getFirstResult()); 
     639                        q.setMaxResults(p.getMaxResults()); 
     640                } else { 
     641                        q.setFirstResult(0); 
     642                        q.setMaxResults(100); 
     643                } 
     644                return q; 
     645        } 
     646         
     647        private static String getLongIdStringForList(final List<Long> projectedIds){ 
     648                if (projectedIds.size() == 0) { 
     649                        return null; 
     650                } 
     651                String  ids = ""; 
     652                for (Long o : projectedIds){ 
     653                        ids += o.toString() + ","; 
     654                } 
     655                if (ids != ""){ 
     656                        ids = ids.substring(0, ids.length()-1); 
     657                } 
     658                return ids; 
     659        } 
     660         
     661        private static String getIntIdStringForList(final List<Integer> projectedIds){ 
     662                if (projectedIds.size() == 0) { 
     663                        return null; 
     664                } 
     665                String  ids = ""; 
     666                for (Integer o : projectedIds){ 
     667                        ids += o.toString() + ","; 
     668                } 
     669                if (ids != ""){ 
     670                        ids = ids.substring(0, ids.length()-1); 
     671                } 
     672                return ids; 
     673        } 
     674         
     675        private static String getLongIdString(final List<Object[]> projectedIds){ 
     676                if (projectedIds.size() == 0) { 
     677                        return null; 
     678                } 
     679                String  ids = ""; 
     680                for (Object[] o : projectedIds){ 
     681                        for (Object id : o){ 
     682                                ids += ((Long)id).toString() + ","; 
     683                        } 
     684                } 
     685                if (ids != ""){ 
     686                        ids = ids.substring(0, ids.length()-1); 
     687                } 
     688                return ids; 
     689        } 
    682690}