// Path: app/models/Files.java
// @Entity
// public class Files extends DomainModel {
// 
// 	public String filepath;
// 	public long file_size;
// 	@ManyToOne
// 	@JoinColumn(name="instance_fk")
// 	public Instance instance;
// 
// 	public String toDownloadString() {
// 		return String.format("%s_%s", instance.series.toDownloadString(), StringUtils.leftPad(instance.inst_no, 3, '0'));
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return String.format("%s", pk);
// 	}
// }
// 
// Path: app/models/Instance.java
// @Entity
// public class Instance extends DomainModel {
// 
// 	public String sop_iuid;
// 	public String sop_cuid;
// 	public byte[] inst_attrs;
// 	public String inst_no;
// 	@ManyToOne
// 	@JoinColumn(name="series_fk")
// 	public Series series;
// 	@OneToMany(mappedBy = "instance")
// 	public Set<Files> files;
// 
// 	@Override
// 	public String toString() {
// 		return String.format("%s", pk);
// 	}
// }
// 
// Path: app/models/Series.java
// @Entity
// public class Series extends DomainModel {
// 
// 	public String series_no;
// 	public String series_iuid;
// 	public String station_name;
// 	public String series_desc;
// 	public String modality;
// 	@ManyToOne
// 	@JoinColumn(name="study_fk")
// 	public Study study;
// 	@OneToMany(mappedBy = "series")
// 	public Set<Instance> instances;
// 
// 	public String toDownloadString() {
// 		return String.format("%s_%s_%s", study.toDownloadString(), series_no, series_desc).replaceAll("\\W+", "");
// 	}
// 
// }


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import models.Files;
import models.Instance;
import models.Series;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.io.FileUtils;
import org.dcm4che.data.Dataset;
import org.dcm4che.data.DcmObjectFactory;
import org.dcm4che.dict.Tags;
import play.Logger;
package util;





public class Dicom {

	public static String attribute(byte[] dataset, String tag) throws IOException {
		Dataset d = DcmObjectFactory.getInstance().newDataset();
		d.readFile(new ByteArrayInputStream(dataset), null, -1);
		return d.getString(Tags.forName(tag));
	}

	public static File file(Instance instance) {
		return new File(Properties.getArchive(), files(instance).filepath);
	}

	static Files files(Instance instance) {
		for (Files files : instance.files) {
			if (new File(Properties.getArchive(), files.filepath).exists()) {
				return files;
			}
		}
		return null;
	}

// Path: app/models/Files.java
// @Entity
// public class Files extends DomainModel {
// 
// 	public String filepath;
// 	public long file_size;
// 	@ManyToOne
// 	@JoinColumn(name="instance_fk")
// 	public Instance instance;
// 
// 	public String toDownloadString() {
// 		return String.format("%s_%s", instance.series.toDownloadString(), StringUtils.leftPad(instance.inst_no, 3, '0'));
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return String.format("%s", pk);
// 	}
// }
// 
// Path: app/models/Instance.java
// @Entity
// public class Instance extends DomainModel {
// 
// 	public String sop_iuid;
// 	public String sop_cuid;
// 	public byte[] inst_attrs;
// 	public String inst_no;
// 	@ManyToOne
// 	@JoinColumn(name="series_fk")
// 	public Series series;
// 	@OneToMany(mappedBy = "instance")
// 	public Set<Files> files;
// 
// 	@Override
// 	public String toString() {
// 		return String.format("%s", pk);
// 	}
// }
// 
// Path: app/models/Series.java
// @Entity
// public class Series extends DomainModel {
// 
// 	public String series_no;
// 	public String series_iuid;
// 	public String station_name;
// 	public String series_desc;
// 	public String modality;
// 	@ManyToOne
// 	@JoinColumn(name="study_fk")
// 	public Study study;
// 	@OneToMany(mappedBy = "series")
// 	public Set<Instance> instances;
// 
// 	public String toDownloadString() {
// 		return String.format("%s_%s_%s", study.toDownloadString(), series_no, series_desc).replaceAll("\\W+", "");
// 	}
// 
// }


// Path: app/util/Dicom.java
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import models.Files;
import models.Instance;
import models.Series;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.io.FileUtils;
import org.dcm4che.data.Dataset;
import org.dcm4che.data.DcmObjectFactory;
import org.dcm4che.dict.Tags;
import play.Logger;

package util;





public class Dicom {

	public static String attribute(byte[] dataset, String tag) throws IOException {
		Dataset d = DcmObjectFactory.getInstance().newDataset();
		d.readFile(new ByteArrayInputStream(dataset), null, -1);
		return d.getString(Tags.forName(tag));
	}

	public static File file(Instance instance) {
		return new File(Properties.getArchive(), files(instance).filepath);
	}

	static Files files(Instance instance) {
		for (Files files : instance.files) {
			if (new File(Properties.getArchive(), files.filepath).exists()) {
				return files;
			}
		}
		return null;
	}

	public static File collate(Series series, boolean preferMultiframe) {