summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/DbImage.java
blob: 386c509c35a531989af28e331b5527babe56109b (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.openslx.imagemaster.db;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.serverconnection.UploadingImage;
import org.openslx.imagemaster.thrift.iface.ImageData;

public class DbImage
{

	public final String uuid;
	public final int imageVersion;
	public final String imageName;
	public final String imagePath;
	public final Timestamp imageCreateTime;
	public final Timestamp imageUpdateTime;
	public final String imageOwner;
	public final String contentOperatingSystem;
	public final boolean isValid;
	public final boolean isDeleted;
	public final String shortDescription;
	public final String longDescription;
	public final Timestamp timestamp;
	public final long fileSize;
	public final String token;
	public final int[] blockStatus;
	public final String serverSessionId;

	public DbImage(String uuid)
	{
		this.uuid = uuid;
		this.imageVersion = 0;
		this.imageName = null;
		this.imagePath = null;
		this.imageCreateTime = null;
		this.imageUpdateTime = null;
		this.imageOwner = null;
		this.contentOperatingSystem = null;
		this.isValid = false;
		this.isDeleted = false;
		this.shortDescription = null;
		this.longDescription = null;
		this.timestamp = new Timestamp( 0 );
		this.fileSize = 0;
		this.token = null;
		this.blockStatus = null;
		this.serverSessionId = null;
	}

	public DbImage(String UUID, int imageVersion, String imageName, String imagePath,
			Timestamp imageCreateTime, Timestamp imageUpdateTime, String imageOwner, String contentOperatingSystem,
			boolean isValid, boolean isDeleted, String shortDescription, String longDescription,
			Timestamp timestamp, long fileSize, String token, String missingBlocksList, String serverSessionId)
	{
		this.uuid = UUID;
		this.imageVersion = imageVersion;
		this.imageName = imageName;
		this.imagePath = imagePath;
		this.imageCreateTime = imageCreateTime;
		this.imageUpdateTime = imageUpdateTime;
		this.imageOwner = imageOwner;
		this.contentOperatingSystem = contentOperatingSystem;
		this.isValid = isValid;
		this.isDeleted = isDeleted;
		this.shortDescription = shortDescription;
		this.longDescription = longDescription;
		this.timestamp = timestamp;
		this.fileSize = fileSize;
		this.token = token;

		String[] parts = missingBlocksList.split( ";" );
		blockStatus = new int[ (int)Math.ceil( this.fileSize / Globals.blockSize ) ];		// initialize array to ones
		for ( int i : blockStatus ) {
			blockStatus[i] = UploadingImage.valid;
		}
		for ( int i = 0; i < parts.length; i++ ) {		// do not copy the last empty string	(1;2;3;) -> "1","2","3",""
			blockStatus[i] = UploadingImage.missing;
		}

		this.serverSessionId = serverSessionId;
	}

	/**
	 * Check if image with imageData already exists. (Only checks the UUID.)
	 * 
	 * @param imageData
	 * @return
	 */
	public static boolean exists( String uuid )
	{
		return getImageByUUID( uuid ) != null;
	}

	/**
	 * Insert a new image into database
	 * 
	 * @param imageData The metadata of the image
	 * @param ts The timestamp of inserting
	 * @param token The token that is able to upload this image
	 * @param missingBlocks The blocks that are missing for this image
	 * @param serverSessionId The server that is uploading this image
	 * @return Affected rows
	 */
	public static int insert( ImageData imageData, long ts, String token, int missingBlocks, String serverSessionId, String filepath )
	{
		Date createTime = new Date( imageData.imageCreateTime );
		Date updateTime = new Date( imageData.imageUpdateTime );
		Date timestamp = new Date( ts );
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

		String missingBlocksList = "";
		for ( int i = 0; i < missingBlocks; i++ ) {
			missingBlocksList = missingBlocksList + String.valueOf( i ) + ";";
		}

		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, timestamp, fileSize, token, missingBlocks, serverSessionId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
						imageData.uuid, imageData.imageVersion, imageData.imageName, filepath,
						sdf.format( createTime ), sdf.format( updateTime ), imageData.imageOwner,
						imageData.contentOperatingSystem, imageData.statusIsValid,
						imageData.statusIsDeleted, imageData.imageShortDescription,
						imageData.imageLongDescription, sdf.format( timestamp ), imageData.fileSize,
						token, missingBlocksList, serverSessionId );
	}

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

	public int updateMissingBlocks( List<Integer> missingBlocks )
	{
		String missingBlocksList = "";
		if ( missingBlocks != null ) {
			for ( Integer block : missingBlocks ) {
				missingBlocksList = missingBlocksList + String.valueOf( block ) + ";";
			}
		}
		return MySQL.update( "UPDATE images SET images.missingBlocks = ? WHERE images.UUID = ?", missingBlocksList, uuid );
	}

	public int delete()
	{
		return MySQL.update( "DELETE FROM images WHERE images.UUID=?", uuid );
	}

	public static List<DbImage> getUploadingImages()
	{
		return MySQL
				.findAll(
						DbImage.class,
						"SELECT images.UUID, images.image_version, images.image_name, images.image_path, images.image_createTime, images.image_updateTime, images.image_owner, images.content_operatingSystem, images.status_isValid, images.status_isDeleted, images.image_shortDescription, images.image_longDescription, images.timestamp, images.fileSize, images.token, images.missingBlocks, images.serverSessionId FROM images WHERE missingBlocks != ?",
						"" );
	}

	public static DbImage getImageByUUID( String uuid )
	{
		return MySQL
				.findUniqueOrNull(
						DbImage.class,
						"SELECT images.UUID, images.image_version, images.image_name, images.image_path, images.image_createTime, images.image_updateTime, images.image_owner, images.content_operatingSystem, images.status_isValid, images.status_isDeleted, images.image_shortDescription, images.image_longDescription, images.timestamp, images.fileSize, images.token, images.missingBlocks, images.serverSessionId FROM images WHERE uuid = ?",
						uuid );
	}

	/**
	 * Creates an instance of the thrift ImageData class of this DbImage object.
	 * 
	 * @return The corresponding image data
	 */
	public ImageData getImageData()
	{
		return new ImageData( this.uuid, this.imageVersion, this.imageName, this.imageCreateTime.getTime(),
				this.imageUpdateTime.getTime(), this.imageOwner, this.contentOperatingSystem, this.isValid,
				this.isDeleted, this.shortDescription, this.longDescription, this.fileSize );
	}
}