001    package org.apache.maven.scm.provider.synergy.consumer;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.log.ScmLogger;
023    import org.apache.maven.scm.provider.synergy.util.SynergyTask;
024    import org.apache.maven.scm.provider.synergy.util.SynergyUtil;
025    import org.apache.maven.scm.util.AbstractConsumer;
026    
027    import java.text.ParseException;
028    import java.text.SimpleDateFormat;
029    import java.util.ArrayList;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.StringTokenizer;
033    
034    /**
035     * Mainly inspired from CruiseControl
036     *
037     * @author <a href="julien.henry@capgemini.com">Julien Henry</a>
038     * @version $Id: SynergyGetCompletedTasksConsumer.java 1054408 2011-01-02 14:05:25Z olamy $
039     */
040    public class SynergyGetCompletedTasksConsumer
041        extends AbstractConsumer
042    {
043    
044        /**
045         * The date format as returned by your installation of CM Synergy. Fri Dec 3
046         * 17:51:56 2004
047         */
048        private String ccmDateFormat = "EEE MMM dd HH:mm:ss yyyy";
049    
050        private String language = "en";
051    
052        private String country = "US";
053    
054        public static final String OUTPUT_FORMAT = "%displayname" + SynergyUtil.SEPARATOR + "%owner"
055            + SynergyUtil.SEPARATOR + "%completion_date" + SynergyUtil.SEPARATOR + "%task_synopsis" + SynergyUtil.SEPARATOR;
056    
057        private List<SynergyTask> entries = new ArrayList<SynergyTask>();
058    
059        /**
060         * @return the tasks
061         */
062        public List<SynergyTask> getTasks()
063        {
064            return entries;
065        }
066    
067        public SynergyGetCompletedTasksConsumer( ScmLogger logger )
068        {
069            super( logger );
070            String dateFormat = System.getProperty( "maven.scm.synergy.ccmDateFormat" );
071            if ( dateFormat != null && !dateFormat.equals( "" ) )
072            {
073                this.ccmDateFormat = dateFormat;
074            }
075            if ( logger.isDebugEnabled() )
076            {
077                logger.debug( "dateFormat = " + this.ccmDateFormat );
078            }
079            String language = System.getProperty( "maven.scm.synergy.language" );
080            if ( language != null && !language.equals( "" ) )
081            {
082                this.language = language;
083            }
084            if ( logger.isDebugEnabled() )
085            {
086                logger.debug( "language = " + this.language );
087            }
088            String country = System.getProperty( "maven.scm.synergy.country" );
089            if ( country != null && !country.equals( "" ) )
090            {
091                this.country = country;
092            }
093            if ( logger.isDebugEnabled() )
094            {
095                logger.debug( "country = " + this.country );
096            }
097        }
098    
099        /** {@inheritDoc} */
100        public void consumeLine( String line )
101        {
102            if ( getLogger().isDebugEnabled() )
103            {
104                getLogger().debug( "Consume: " + line );
105            }
106            StringTokenizer tokenizer = new StringTokenizer( line.trim(), SynergyUtil.SEPARATOR );
107            if ( tokenizer.countTokens() == 4 )
108            {
109                SynergyTask task = new SynergyTask();
110                task.setNumber( Integer.parseInt( tokenizer.nextToken() ) );
111                task.setUsername( tokenizer.nextToken() );
112                try
113                {
114                    task.setModifiedTime( new SimpleDateFormat( ccmDateFormat, new Locale( language, country ) )
115                        .parse( tokenizer.nextToken() ) );
116                }
117                catch ( ParseException e )
118                {
119                    if ( getLogger().isErrorEnabled() )
120                    {
121                        getLogger().error( "Wrong date format", e );
122                    }
123                }
124                task.setComment( tokenizer.nextToken() );
125    
126                // Add the task to the list
127                entries.add( task );
128            }
129            else
130            {
131                if ( getLogger().isErrorEnabled() )
132                {
133                    getLogger().error(
134                                       "Invalid token count in SynergyGetCompletedTasksConsumer ["
135                                           + tokenizer.countTokens() + "]" );
136                }
137    
138                if ( getLogger().isDebugEnabled() )
139                {
140                    while ( tokenizer.hasMoreElements() )
141                    {
142                        getLogger().debug( tokenizer.nextToken() );
143                    }
144                }
145            }
146    
147        }
148    }