summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/mappers/DbImage.java
blob: f4c3ddc341694a6668b84abd77a2938841a64451 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.openslx.imagemaster.db.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.ImagePublishData;
import org.openslx.bwlp.thrift.iface.InvocationError;
import org.openslx.bwlp.thrift.iface.TInvocationException;
import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.db.Database;
import org.openslx.imagemaster.db.MysqlConnection;
import org.openslx.imagemaster.db.MysqlStatement;
import org.openslx.util.Util;

/**
 * Representing an image in the database.
 * Is used to modify/update database entries.
 */
public class DbImage
{

	private static final Logger LOGGER = Logger.getLogger( DbImage.class );

	public static ImagePublishData getImageVersion( String imageVersionId )
	{
		return null;
	}

	public static void createImageBase( ImagePublishData img ) throws TInvocationException
	{
		// Input seems valid
		try ( MysqlConnection connection = Database.getConnection() ) {
			MysqlStatement stmt = connection.prepareStatement( "SELECT virtid FROM imagebase WHERE imagebaseid = :baseid" );
			stmt.setString( "baseid", img.imageBaseId );
			ResultSet rs = stmt.executeQuery();
			if ( rs.next() ) {
				if ( !img.virtId.equals( rs.getString( "virtid" ) ) ) {
					throw new TInvocationException( InvocationError.INVALID_DATA, "Virtualizer id mismatch" );
				}
				MysqlStatement stmt2 = connection.prepareStatement( "UPDATE imagebase SET"
						+ " displayname = :displayname, updaterid = :updaterid,"
						+ " description = :description, osid = :osid, updatetime = UNIX_TIMESTAMP(),"
						+ " istemplate = :istemplate WHERE imagebaseid = :baseid" );
				stmt2.setString( "baseid", img.imageBaseId );
				stmt2.setString( "displayname", img.imageName );
				stmt2.setString( "updaterid", img.user.userId );
				stmt2.setString( "description", img.description );
				stmt2.setInt( "osid", img.osId );
				stmt2.setBoolean( "istemplate", img.isTemplate );
				stmt2.executeUpdate();
			} else {
				MysqlStatement stmt2 = connection.prepareStatement( "INSERT INTO imagebase"
						+ " (imagebaseid, latestversionid, displayname, description, osid,"
						+ "  virtid, createtime, updatetime, ownerid, updaterid, istemplate)"
						+ "                   VALUES                     "
						+ " (:imagebaseid, NULL, :displayname, :description, :osid,"
						+ "  :virtid, :createtime, UNIX_TIMESTAMP(), :ownerid, :updaterid, :istemplate)" );
				stmt2.setString( "imagebaseid", img.imageBaseId );
				stmt2.setString( "displayname", img.imageName );
				stmt2.setString( "description", img.description );
				stmt2.setInt( "osid", img.osId );
				stmt2.setString( "virtid", img.virtId );
				stmt2.setLong( "createtime", img.createTime );
				stmt2.setString( "ownerid", img.user.userId );
				stmt2.setString( "updaterid", img.user.userId );
				stmt2.setBoolean( "istemplate", img.isTemplate );
				stmt2.executeUpdate();
			}
			connection.commit();
		} catch ( SQLException e ) {
			LOGGER.error( "Query failed in DbImage.createImageBase()", e );
			throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Database boo-boo" );
		}
	}

	public static void createImageVersion( ImagePublishData img, String relLocalPath ) throws SQLException
	{
		try ( MysqlConnection connection = Database.getConnection() ) {
			// Insert version
			MysqlStatement verStmt = connection.prepareStatement( "INSERT INTO imageversion"
					+ " (imageversionid, imagebaseid, createtime, expiretime, filesize,"
					+ "  filepath, uploaderid, isvalid, isprocessed, mastersha1, virtualizerconfig)"
					+ "                        VALUES                          "
					+ " (:imageversionid, :imagebaseid, :createtime, :expiretime, :filesize,"
					+ "  :filepath, :uploaderid, 0, 0, NULL, NULL)" );
			verStmt.setString( "imageversionid", img.imageVersionId );
			verStmt.setString( "imagebaseid", img.imageBaseId );
			verStmt.setLong( "createtime", img.createTime );
			verStmt.setLong( "expiretime", Util.unixTime() + Globals.getImageValiditySeconds() );
			verStmt.setLong( "filesize", img.fileSize );
			verStmt.setString( "filepath", relLocalPath );
			verStmt.setString( "uploaderid", img.user.userId );
			verStmt.execute();
			connection.commit();
		} catch ( SQLException e ) {
			LOGGER.error( "Query failed in DbImage.createImageVersion()", e );
			throw e;
		}
	}

}