summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/DbImage.java
blob: bf643bbeec2b5b734082c0250bcf1c335b358cbe (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
package org.openslx.imagemaster.db;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.openslx.imagemaster.thrift.iface.ImageData;

public class DbImage
{

	private String UUID;

	public DbImage(String UUID)
	{
		this.UUID = UUID;
	}

	/**
	 * Check if image with imageData already exists. (Only checks the UUID.)
	 * 
	 * @param imageData
	 * @return
	 */
	public static boolean exists( ImageData imageData )
	{
		if ( MySQL.findUniqueOrNull( DbImage.class,
				"SELECT images.UUID FROM images WHERE images.UUID = ?",
				imageData.uuid ) == null ) {
			return false;
		} else {
			return true;
		}
	}

	public static int insert( ImageData imageData )
	{
		Date createTime = new Date( imageData.imageCreateTime );
		Date updateTime = new Date( imageData.imageUpdateTime );
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

		int ownerId = DbUser.getUserIdByName( imageData.imageOwner );

		return MySQL
				.update(
						"INSERT INTO images (UUID, image_version, image_name, image_path, image_createTime, image_updateTime, image_owner, content_operatingSystem, status_isValid, status_isDeleted, image_shortDescription, image_longDescription) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
						imageData.uuid, imageData.imageVersion, imageData.imageName, "!uploading!",
						sdf.format( createTime ), sdf.format( updateTime ), ownerId,
						imageData.conentOperatingSystem, imageData.statusIsValid,
						imageData.statusIsDeleted, imageData.imageShortDescription,
						imageData.imageLongDescription );
	}

	public String getUUID()
	{
		return this.UUID;
	}

	public static int update( ImageData imageData, String location )
	{
		return MySQL.update( "UPDATE images SET images.image_path = ? WHERE images.UUID = ?", location, imageData.uuid );
	}
}