Created
March 27, 2013 14:27
-
-
Save maxcom/5254570 to your computer and use it in GitHub Desktop.
Конвертер профилей в БД
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lor.profiletool | |
import java.io.{IOException, FileInputStream, ObjectInputStream, File} | |
import java.util | |
import collection.JavaConversions._ | |
import java.sql.DriverManager | |
object Tool { | |
def main(args: Array[String]) { | |
if (args.length!=2) { | |
println("Usage: profiletool <profile dir> <jdbc url>") | |
sys.exit(-1) | |
} | |
val dir = new File(args(0)) | |
val allProfiles = dir.listFiles().view.map(file => | |
new Profile(file.getName, readProfile(file)) | |
).filter(x => !x.isEmpty) | |
val db = DriverManager.getConnection(args(1)) | |
val st = db.prepareStatement("SELECT id FROM users WHERE nick=?") | |
val insert = db.prepareStatement("INSERT INTO user_settings (id, settings, main, lastmod) VALUES (?,?,?,CURRENT_TIMESTAMP)") | |
for (profile <- allProfiles) { | |
st.setString(1, profile.username) | |
val rs = st.executeQuery() | |
if (rs.next()) { | |
val id = rs.getInt("id") | |
println(id + " settings: " + profile.stringSettings + " mainPage: " +profile.mainPage) | |
insert.clearParameters() | |
insert.setInt(1, id) | |
insert.setObject(2, mapAsJavaMap(profile.stringSettings)) | |
insert.setObject(3, profile.mainPage match { | |
case Some(x) => db.createArrayOf("text", seqAsJavaList(x).toArray) | |
case None => null | |
}) | |
if (insert.executeUpdate()!=1) { | |
println("can't insert...") | |
} | |
} else { | |
println("User "+profile.username+" not found!") | |
} | |
} | |
} | |
def readProfile(file: File) = { | |
try { | |
val is = new ObjectInputStream(new FileInputStream(file)) | |
try { | |
is.readObject().asInstanceOf[util.Map[String, Object]].toMap | |
} finally { | |
is.close() | |
} | |
} catch { | |
case e:IOException => { | |
println("Bad profile " + file) | |
Map[String, Object]() | |
} | |
} | |
} | |
} | |
class Profile (val username: String, settings: Map[String, Object]) { | |
override def toString = username + " settings:" + settings +" main2:"+mainPage | |
def stringSettings = | |
settings.filter( x => x match { | |
case (_,_:util.List[_]) => false | |
case ("system.timestamp",_) => false | |
case ("ProfileName",_) => false | |
case ("DebugMode",_) => false | |
case ("style",_) => false | |
case _ => true | |
} ).map{ case (k,v) => k -> v.toString } | |
def mainPage = | |
settings.get("main2") match { | |
case Some(list:util.List[_]) => Some(list.toList) | |
case None => None | |
case _ => throw new RuntimeException("bad main2 value") | |
} | |
def isEmpty = | |
stringSettings.isEmpty && mainPage.isEmpty | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment