001    /*----------------------------------------------------------------------------
002     This file is part of deegree, http://deegree.org/
003     Copyright (C) 2001-2009 by:
004       Department of Geography, University of Bonn
005     and
006       lat/lon GmbH
007    
008     This library is free software; you can redistribute it and/or modify it under
009     the terms of the GNU Lesser General Public License as published by the Free
010     Software Foundation; either version 2.1 of the License, or (at your option)
011     any later version.
012     This library is distributed in the hope that it will be useful, but WITHOUT
013     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
014     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
015     details.
016     You should have received a copy of the GNU Lesser General Public License
017     along with this library; if not, write to the Free Software Foundation, Inc.,
018     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019    
020     Contact information:
021    
022     lat/lon GmbH
023     Aennchenstr. 19, 53177 Bonn
024     Germany
025     http://lat-lon.de/
026    
027     Department of Geography, University of Bonn
028     Prof. Dr. Klaus Greve
029     Postfach 1147, 53001 Bonn
030     Germany
031     http://www.geographie.uni-bonn.de/deegree/
032    
033     e-mail: info@deegree.org
034    ----------------------------------------------------------------------------*/
035    
036    package org.deegree.ogcwebservices.csw;
037    
038    import java.sql.Connection;
039    import java.sql.PreparedStatement;
040    import java.sql.ResultSet;
041    import java.sql.SQLException;
042    import java.sql.Statement;
043    import java.util.ArrayList;
044    import java.util.HashMap;
045    import java.util.List;
046    import java.util.Map;
047    import java.util.Timer;
048    import java.util.TimerTask;
049    
050    import org.deegree.framework.log.ILogger;
051    import org.deegree.framework.log.LoggerFactory;
052    import org.deegree.framework.mail.EMailMessage;
053    import org.deegree.framework.mail.MailHelper;
054    import org.deegree.framework.mail.SendMailException;
055    import org.deegree.framework.mail.UnknownMimeTypeException;
056    import org.deegree.i18n.Messages;
057    
058    /**
059     * <code>DetectUnvalidCouplings</code>
060     *
061     * @author <a href="mailto:buesching@lat-lon.de">Lyn Buesching</a>
062     * @author last edited by: $Author:$
063     *
064     * @version $Revision:$, $Date:$
065     *
066     */
067    public class DetectInvalidCouplings {
068    
069        private static final ILogger LOG = LoggerFactory.getLogger( DetectInvalidCouplings.class );
070    
071        private static final DetectInvalidCouplings instance = new DetectInvalidCouplings();
072    
073        private Timer timer;
074    
075        private DetectInvalidCouplings() {
076            timer = new Timer();
077            timer.schedule( new DetectInvalidCouplingsTask(), 1, 86400000 );
078        }
079    
080        public static DetectInvalidCouplings getInstance() {
081            return instance;
082        }
083    
084        private class DetectInvalidCouplingsTask extends TimerTask {
085            /*
086             * (non-Javadoc)
087             *
088             * @see java.util.TimerTask#run()
089             */
090            @Override
091            public void run() {
092                try {
093                    Connection conn = CSW202PropertiesAccess.getConnection();
094                    if ( conn != null ) {
095                        Map<String, List<String>> result = new HashMap<String, List<String>>();
096                        Statement st = conn.createStatement();
097                        ResultSet rsUuids = st.executeQuery( "SELECT distinct(uuidref) FROM operateson WHERE uuidref IS NOT null" );
098    
099                        while ( rsUuids.next() ) {
100                            String uuidref = rsUuids.getString( "uuidref" );
101                            PreparedStatement psCount = conn.prepareStatement( "SELECT count(fileIdentifier) as count FROM fileIdentifier WHERE fileIdentifier = ?" );
102                            psCount.setString( 1, uuidref );
103                            ResultSet rsCount = psCount.executeQuery();
104                            if ( !rsCount.next() || rsCount.getInt( "count" ) == 0 ) {
105                                List<String> tmp;
106                                PreparedStatement psIds = conn.prepareStatement( "SELECT f.fileidentifier as id FROM fileidentifier f, md_metadata m, operateson o WHERE o.fk_serviceidentification = m.id AND o.uuidref =  ? AND o.fk_serviceidentification = m.id AND m.fk_fileidentifier = f.id" );
107                                psIds.setString( 1, uuidref );
108                                ResultSet rsIds = psIds.executeQuery();
109                                while ( rsIds.next() ) {
110                                    String id = rsIds.getString( "id" );
111                                    if ( result.containsKey( uuidref ) ) {
112                                        tmp = result.get( uuidref );
113                                    } else {
114                                        tmp = new ArrayList<String>();
115                                    }
116                                    tmp.add( id );
117                                    result.put( uuidref, tmp );
118                                }
119                            }
120                        }
121                        printResult( result );
122                        conn.close();
123                    }
124                } catch ( SQLException e ) {
125                    LOG.logError( "Could not connect with database", e );
126                }
127            }
128    
129            private void printResult( Map<String, List<String>> result ) {
130                StringBuffer sb = new StringBuffer();
131                if ( result != null && result.size() > 0 ) {
132                    sb.append( Messages.getMessage( "CSW_INVALID_SERVICES_DETECTED" ) );
133                    for ( String uuidref : result.keySet() ) {
134                        List<String> uuidrefs = result.get( uuidref );
135                        String ids = "";
136                        for ( String id : uuidrefs ) {
137                            if ( ids.length() > 0 ) {
138                                ids = ids + ", ";
139                            }
140                            ids = ids + id;
141                        }
142                        sb.append( Messages.getMessage( "CSW_INVALID_SERVICES_DETECTED_ENTRY", ids, uuidref ) );
143                    }
144    
145                    try {
146                        sendMail( sb.toString() );
147                    } catch ( SendMailException e ) {
148                        LOG.logError( "Could not send inform admin about unknown coupled resources!", e );
149                    }
150                } else {
151                    sb.append( Messages.getMessage( "CSW_NO_INVALID_SERVICES_DETECTED" ) );
152                }
153                LOG.logInfo( sb.toString() );
154            }
155        }
156    
157        private void sendMail( String message )
158                                throws SendMailException {
159            String sender = "do-not-return@deegree-csw.de";
160            String receiver = CSW202PropertiesAccess.getString( "admin.email" );
161            String subject = "Detected some unknown coupled resources";
162            EMailMessage mm = new EMailMessage( sender, receiver, subject, message );
163            try {
164                mm.setMimeType( "text/html" );
165            } catch ( UnknownMimeTypeException e ) {
166                LOG.logError( e.getMessage() );
167            }
168            String smtpHost = CSW202PropertiesAccess.getString( "smtphost" );
169            MailHelper.createAndSendMail( mm, smtpHost );
170        }
171    
172        public static void main( String[] args ) {
173            // Class.forName( "DetectInvalidCouplings");
174        }
175    
176    }