mx.org.cedn.avisosconagua.mongo.UpdateIssueDate.java Source code

Java tutorial

Introduction

Here is the source code for mx.org.cedn.avisosconagua.mongo.UpdateIssueDate.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Mxico Abierto
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information visit https://github.com/mxabierto/avisos.
*/
package mx.org.cedn.avisosconagua.mongo;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Script para actualizar la base, invocar con:
 * java -cp target/dependency/mongo-java-driver-2.11.4.jar:target/classes/ mx.org.cedn.avisosconagua.mongo.UpdateIssueDate
 * 
 * Script to update database, to run:
 * java -cp target/dependency/mongo-java-driver-2.11.4.jar:target/classes/ mx.org.cedn.avisosconagua.mongo.UpdateIssueDate
 * @author serch
 */
public class UpdateIssueDate {

    public static void main(String[] arg) throws Exception {
        MongoClientURI mongoClientURI = new MongoClientURI(System.getenv("MONGOHQ_URL"));
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        DB mongoDB = mongoClient.getDB(mongoClientURI.getDatabase());
        String GENERATED_COL = "GeneratedFiles";
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        SimpleDateFormat isoformater = new SimpleDateFormat("YYYY-MM-dd HH:mm");
        if (null != mongoClientURI.getUsername()) {
            mongoDB.authenticate(mongoClientURI.getUsername(), mongoClientURI.getPassword());
        }
        DBCollection col = mongoDB.getCollection(GENERATED_COL);
        DBCursor cursor = col.find();
        for (DBObject obj : cursor) {
            String date = (String) obj.get("issueDate");
            Date fec = null;
            try {
                fec = sdf.parse(date);
            } catch (ParseException npe) {

            }
            if (null != fec) {
                date = isoformater.format(fec);
                DBObject act = col.findOne(obj);
                obj.put("issueDate", date);
                col.update(act, obj);
            }
        }
    }
}

//        ArrayList<String>lista=getInstance().listPublishedAdvices(100);
//        ArrayList<String>newList = new ArrayList<>();
//        DBCollection col = getInstance().mongoDB.getCollection(GENERATED_COL);
//        for (String id: lista){
//            newList.add(Utils.getISODate((String)getInstance().getPublishedAdvice(id).get("issueDate"))+" "+id);
//            BasicDBObject bdbo =(BasicDBObject)col.findOne(new BasicDBObject(INTERNAL_FORM_ID, id));
//            bdbo.append("issueDate", Utils.getISODate(bdbo.getString("issueDate")));
//            
//            mongoDB.getCollection(GENERATED_COL).findOne(new BasicDBObject(INTERNAL_FORM_ID, adviceId))
//                    
//            System.out.println("id:"+id+" "+Utils.getISODate((String)getInstance().getPublishedAdvice(id).get("issueDate")));
//        }
//        Collections.sort(newList);
//        Collections.reverse(newList);
//        for(String id:newList){
//            System.out.println("id: "+ id);
//        }