001    //$HeadURL: https://sushibar/svn/deegree/base/trunk/src/org/deegree/framework/xml/Arc2ISO.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005     Department of Geography, University of Bonn
006     and
007     lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035     ----------------------------------------------------------------------------*/
036    
037    package org.deegree.ogcwebservices.csw.iso_profile;
038    
039    import java.sql.Connection;
040    import java.sql.PreparedStatement;
041    import java.sql.ResultSet;
042    import java.sql.SQLException;
043    import java.util.UUID;
044    
045    import org.deegree.framework.log.ILogger;
046    import org.deegree.framework.log.LoggerFactory;
047    import org.deegree.ogcwebservices.csw.CSW202PropertiesAccess;
048    
049    /**
050     * <code>Coupling</code>
051     * 
052     * @author <a href="mailto:buesching@lat-lon.de">Lyn Buesching</a>
053     * @author last edited by: $Author:$
054     * 
055     * @version $Revision:$, $Date:$
056     * 
057     */
058    public class Coupling {
059    
060        private static final ILogger LOG = LoggerFactory.getLogger( Coupling.class );
061    
062        /**
063         * @param uuidref
064         *            the uuidref
065         * @return returns true, if an dataset or series with the given uuid of the
066         *         MD_DataIdentification element exists
067         */
068        public static boolean existsRecord( String uuidref ) {
069            try {
070                Connection conn = CSW202PropertiesAccess.getConnection();
071                if ( conn != null ) {
072                    String sql = "SELECT count(d.uuid) as count FROM md_metadata m, hierarchylevelcode h, md_dataidentification d WHERE (h.codelistvalue  = 'dataset' or  h.codelistvalue  = 'series' ) and m.fk_hlevelcode = h.id and m.id = d.id and d.uuid = ?";
073                    LOG.logDebug( "Request CSW if dataset or series with MD_DataIdentification/@uuid " + uuidref
074                                  + " exists [" + sql + "]" );
075                    PreparedStatement st = conn.prepareStatement( sql );
076                    st.setString( 1, uuidref );
077                    ResultSet rs = st.executeQuery();
078                    if ( rs.next() && rs.getInt( "count" ) > 0 ) {
079                        return true;
080                    }
081                }
082            } catch ( SQLException e ) {
083                LOG.logError( "could not request dataidentification id for fileidentifier " + uuidref, e );
084            }
085    
086            return false;
087        }
088    
089        /**
090         * @return the url of the service
091         */
092        public static String getCSWUrl() {
093            return CSW202PropertiesAccess.getString( "csw.url" );
094        }
095    
096        /**
097         * Replaces all ':' with '_' and added the prefix "uuid_" if the given uuid begins with a number
098         * or invalid char.
099         * 
100         * @param uuid
101         *            the uuid
102         * @return the uuid if it is a valid id, otherwise a modified version of the uuid
103         */
104        public static String getValidId( String uuid ) {
105            uuid = uuid.replace( ':', '_' );
106            String first = uuid.substring( 0, 1 );
107            if ( !first.matches( "[a-zA-Z_]" ) ) {
108                return "uuid_" + uuid;
109            }
110            return uuid;
111        }
112    
113        /**
114         * @return a random created id, based on a uuid
115         */
116        public static String createValidId() {
117            return getValidId( UUID.randomUUID().toString() );
118        }
119    
120    }