1   package com.trendmicro.grid.acl.ds.dummy;
2   
3   import com.trendmicro.grid.acl.ds.PackageProvider;
4   import com.trendmicro.grid.acl.ds.TagQueryExpression;
5   import com.trendmicro.grid.acl.ds.datatypes.*;
6   import com.trendmicro.grid.acl.l0.datatypes.FileIdentiferListPage;
7   import com.trendmicro.grid.acl.l0.datatypes.FileIdentifier;
8   import com.trendmicro.grid.acl.l0.datatypes.NameListPage;
9   import com.trendmicro.grid.acl.l0.datatypes.Range;
10  import com.trendmicro.grid.acl.metadata.Meta;
11  import com.trendmicro.grid.acl.metadata.Metadata;
12  import org.springframework.stereotype.Repository;
13  
14  import java.util.Arrays;
15  import java.util.Collection;
16  import java.util.Collections;
17  import java.util.Date;
18  
19  /**
20   * Implements a dummy in-memory provider for packages.
21   *
22   * @author juergen_kellerer, 2010-05-07
23   * @version 1.0
24   */
25  @Repository
26  public class DummyPackageProvider implements PackageProvider {
27  
28  	NameListPage dummyPackageNames = new NameListPage(0, true,
29  			"dummy inc:dummy:1-0-0-1000:windows:x86:en",
30  			"dummy inc:dummy:1-1-0-1000:windows:x64:en",
31  			"dummy inc:dummy:2-1-0-1000:windows:x64:en");
32  
33  	FileIdentiferListPage dummyFileId = new FileIdentiferListPage(0, true,
34  			new FileIdentifier(new byte[20]), new FileIdentifier(new byte[20]));
35  
36  	SharedFileInformation dummyFileInformation = new SharedFileInformation(
37  			new Date(), new Date(), new Date(), new String[]{"tag1", "tag2"}, 3, 631);
38  
39  	SharedPackageInformation dummyPackageInformation = new SharedPackageInformation(
40  			"dummy inc:dummy:1-0-0-1000:windows:x86:en", "Dummy Package", "dummy", "dummy inc",
41  			new String[]{"tag1", "tag2", "tag3"}, dummyFileInformation);
42  
43  	Metadata dummyMetadata = new Metadata(Arrays.asList(
44  			new Meta("dummyKey1").value("DummyValue1"),
45  			new Meta("dummyKey2").value(new Date()),
46  			new Meta("dummyKey3").value(354324.34D),
47  			new Meta("dummyKey4").value("value1", "value2")
48  	));
49  
50  	SharedVendor dummyVendor = new SharedVendor("dummy inc", "Dummy Inc.", new Date(), dummyMetadata);
51  
52  	SharedPackageFamily dummyPackageFamily = new SharedPackageFamily(dummyVendor, "dummy", "Dummy", dummyMetadata);
53  
54  	SharedFileDetails dummyFileDetails = new SharedFileDetails(dummyFileId.getElements().get(0),
55  			dummyFileInformation, dummyMetadata);
56  
57  	SharedPackageDetails dummyPackageDetails = new SharedPackageDetails(
58  			dummyPackageFamily, dummyPackageInformation, dummyMetadata,
59  			new SharedFileMetadata(dummyFileDetails.getIdentifier(), dummyFileDetails.getMetadata()));
60  
61  	/**
62  	 * {@inheritDoc}
63  	 */
64  	public NameListPage getPackageNamesInFamily(String packageFamilyName, int pageNumber) {
65  		return "dummy".equals(packageFamilyName) && pageNumber == 0 ? dummyPackageNames : null;
66  	}
67  
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public Collection<Boolean> isPackagesTaggedWithById(Collection<FileIdentifier> files, String[] tags) {
72  		return Collections.nCopies(files.size(), false);
73  	}
74  
75  	/**
76  	 * {@inheritDoc}
77  	 */
78  	public Collection<Boolean> isPackagesTaggedWithByName(Collection<String> packageNames, String[] tags) {
79  		return Collections.nCopies(packageNames.size(), false);
80  	}
81  
82  	/**
83  	 * {@inheritDoc}
84  	 */
85  	public NameListPage getPackageNamesTaggedWith(String[] tags, int pageNumber) {
86  		return pageNumber > 0 ? null : new NameListPage(0, true);
87  	}
88  
89  	/**
90  	 * {@inheritDoc}
91  	 */
92  	public NameListPage getMatchingPackageNames(TagQueryExpression expression, Range range, int pageNumber) {
93  		return getPackageNamesTaggedWith(null, pageNumber);
94  	}
95  
96  	/**
97  	 * {@inheritDoc}
98  	 */
99  	public NameListPage getReferencingPackageNamesById(FileIdentifier file, int pageNumber) {
100 		return getPackageNamesTaggedWith(null, pageNumber);
101 	}
102 
103 	/**
104 	 * {@inheritDoc}
105 	 */
106 	public NameListPage getReferencingPackageNames(String packageName, int pageNumber) {
107 		return getPackageNamesTaggedWith(null, pageNumber);
108 	}
109 
110 	/**
111 	 * {@inheritDoc}
112 	 */
113 	public SharedNamedFileIdentifierListPage getFilesContainedInPackageById(FileIdentifier packageFile, int pageNumber) {
114 		return pageNumber > 0 ? null : new SharedNamedFileIdentifierListPage(0, true);
115 	}
116 
117 	/**
118 	 * {@inheritDoc}
119 	 */
120 	public SharedNamedFileIdentifierListPage getFilesContainedInPackageByName(String packageName, int pageNumber) {
121 		return getFilesContainedInPackageById(null, pageNumber);
122 	}
123 
124 	/**
125 	 * {@inheritDoc}
126 	 */
127 	public NameListPage getPackagesContainedInPackageById(FileIdentifier packageFile, int pageNumber) {
128 		return getPackageNamesTaggedWith(null, pageNumber);
129 	}
130 
131 	/**
132 	 * {@inheritDoc}
133 	 */
134 	public NameListPage getPackagesContainedInPackageByName(String packageName, int pageNumber) {
135 		return getPackageNamesTaggedWith(null, pageNumber);
136 	}
137 
138 	/**
139 	 * {@inheritDoc}
140 	 */
141 	public Collection<SharedPackageInformation> getPackageInformationListById(Collection<FileIdentifier> files) {
142 		return Collections.nCopies(files.size(), dummyPackageInformation);
143 	}
144 
145 	/**
146 	 * {@inheritDoc}
147 	 */
148 	public Collection<SharedPackageInformation> getPackageInformationListByName(Collection<String> packageNames) {
149 		return Collections.nCopies(packageNames.size(), dummyPackageInformation);
150 	}
151 
152 	/**
153 	 * {@inheritDoc}
154 	 */
155 	public Collection<SharedPackageDetails> getPackageDetailsListById(Collection<FileIdentifier> files) {
156 		return Collections.nCopies(files.size(), dummyPackageDetails);
157 	}
158 
159 	/**
160 	 * {@inheritDoc}
161 	 */
162 	public Collection<SharedPackageDetails> getPackageDetailsListByName(Collection<String> packageNames) {
163 		return Collections.nCopies(packageNames.size(), dummyPackageDetails);
164 	}
165 
166 	/**
167 	 * {@inheritDoc}
168 	 */
169 	public Collection<FileIdentifier> getPackageFileIdentifiersByName(Collection<String> packageNames) {
170 		FileIdentifier id = dummyPackageDetails.getFileMetadata().getIdentifier();
171 		return Collections.nCopies(packageNames.size(), id);
172 	}
173 }