summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/DbImage.java
blob: 18a1cc8eb84cb7821eef355f23db0a90d097a507 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package org.openslx.imagemaster.db;

import java.util.List;

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

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

	public final String uuid;
	public final int imageVersion;
	public final String imageName;
	/**
	 * Relative path of image file (relative to Globals.getImageDir())
	 */
	public final String imagePath;
	public final long imageCreateTime;
	public final long imageUpdateTime;
	public final int imageOwnerId;
	public final String contentOperatingSystem;
	public final boolean isValid;
	public final boolean isDeleted;
	public final String shortDescription;
	public final String longDescription;
	public final long fileSize;
	public final int[] blockStatus;

	public DbImage( String uuid )
	{
		this.uuid = uuid;
		this.imageVersion = 0;
		this.imageName = null;
		this.imagePath = null;
		this.imageCreateTime = 0;
		this.imageUpdateTime = 0;
		this.imageOwnerId = 0;
		this.contentOperatingSystem = null;
		this.isValid = false;
		this.isDeleted = false;
		this.shortDescription = null;
		this.longDescription = null;
		this.fileSize = 0;
		this.blockStatus = null;
	}

	public DbImage( String uuid, int imageVersion, String imageName, String imagePath,
			long imageCreateTime, long imageUpdateTime, int imageOwnerId, String contentOperatingSystem,
			boolean isValid, boolean isDeleted, String shortDescription, String longDescription,
			long fileSize, String missingBlocksList )
	{
		this.uuid = uuid;
		this.imageVersion = imageVersion;
		this.imageName = imageName;
		this.imagePath = imagePath;
		this.imageCreateTime = imageCreateTime;
		this.imageUpdateTime = imageUpdateTime;
		this.imageOwnerId = imageOwnerId;
		this.contentOperatingSystem = contentOperatingSystem;
		this.isValid = isValid;
		this.isDeleted = isDeleted;
		this.shortDescription = shortDescription;
		this.longDescription = longDescription;
		this.fileSize = fileSize;

		String[] parts = missingBlocksList.split( ";" );
		blockStatus = new int[ Util.getNumberOfBlocks( fileSize, Globals.blockSize ) ];		// initialize array to ones
		for ( int i = 0; i < blockStatus.length; ++i ) {
			blockStatus[i] = UploadingImage.VALID;
		}
		for ( String block : parts ) { // Now mark missing blocks (if any)
			int i = Util.tryToParseInt( block, -1 );
			if ( i >= 0 && i < blockStatus.length )
				blockStatus[i] = UploadingImage.MISSING;
		}
	}

	/**
	 * 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 filepath Local storage path of image
	 * @return Affected rows
	 */
	public static int insert( ImageData imageData, String filepath )
	{
		int numBlocks = Util.getNumberOfBlocks( imageData.fileSize, Globals.blockSize );
		String missingBlocksList = "";
		for ( int i = 0; i < numBlocks; i++ ) {
			missingBlocksList = missingBlocksList + String.valueOf( i ) + ";";
		}
		DbUser user = DbUser.forLogin( imageData.imageOwner );
		int owner = 0;
		if (user != null)
			owner = user.userId;

		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, fileSize, missingBlocks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
						imageData.uuid, imageData.imageVersion, imageData.imageName, filepath,
						imageData.imageCreateTime, imageData.imageUpdateTime, owner,
						imageData.contentOperatingSystem, imageData.statusIsValid,
						imageData.statusIsDeleted, imageData.imageShortDescription,
						imageData.imageLongDescription, imageData.fileSize,
						missingBlocksList );
	}

	/**
	 * Updates the missing blocks of an uploading image.
	 * 
	 * @param missingBlocks
	 * @return
	 */
	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 );
	}

	/**
	 * Updates the database with a new version of an image and its missing blocks.
	 * 
	 * @param version
	 * @param amountBlocks
	 */
	public void updateVersion( int version, int amountBlocks )
	{
		if ( version <= 0 || amountBlocks <= 0 )
			return;
		String missingBlocksList = "";
		for ( int i = 0; i < amountBlocks; i++ ) {
			missingBlocksList = missingBlocksList + String.valueOf( i ) + ";";
		}
		MySQL.update( "UPDATE images SET images.missingBlocks, images.version VALUES (?, ?)", missingBlocksList, version );
	}

	/**
	 * Marks an image as _deleted_ in the database.
	 * 
	 * @return
	 */
	public int delete()
	{
		return MySQL.update( "UPDATE images SET images.isDeleted=? WHERE images.UUID=?", true, uuid );
	}

	/**
	 * Returns all images from database where blocks are still missing.
	 * 
	 * @return
	 */
	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 != ?",
						"" );
	}

	/**
	 * Returns the image that is corrsponding to a specified uuid.
	 * 
	 * @param uuid
	 * @return
	 */
	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.fileSize, images.missingBlocks 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()
	{
		String owner = "unknown";
		DbUser user = DbUser.forLogin( this.imageOwnerId );
		if (user != null)
			owner = user.getLogin();
		return new ImageData( this.uuid, this.imageVersion, this.imageName, this.imageCreateTime,
				this.imageUpdateTime, owner, this.contentOperatingSystem, this.isValid,
				this.isDeleted, this.shortDescription, this.longDescription, this.fileSize );
	}
	
	/**
	 * Get absolute path of this image
	 *
	 * @return absolute path
	 */
	public String getAbsolutePath()
	{
		return Globals.getImageDir() + "/" + this.imagePath;
	}
	
}