-
Notifications
You must be signed in to change notification settings - Fork 973
/
MetadataRetrieveController.cls
200 lines (181 loc) · 7.93 KB
/
MetadataRetrieveController.cls
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
/**
* Copyright (c) 2012, FinancialForce.com, inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the FinancialForce.com, inc nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
/**
* This example demonstrates how to utilise the Metadata API Retrieve operation inconjunction with the JSZip JavaScript library
**/
public with sharing class MetadataRetrieveController
{
private static final Integer METADATA_API_VERSION =
Integer.valueOf(new MetadataService.MetadataPort().endpoint_x.substringAfterLast('/'));
public List<SelectOption> MetaDataTypes {get; set;}
public String MetaDataType {get; set;}
public List<SelectOption> MetaDataItems {get; set;}
public String MetaDataFolder {get; set;}
public String MetaDataItem {get; set;}
public String MetadataFileName {get; set;}
public String MetadataFileData {get; set;}
public MetadataService.AsyncResult AsyncResult {get; private set;}
public String MetaDataRetrieveZip { get; private set; }
public List<MetadataFile> MetadataFiles { get; set; }
public PageReference init()
{
// List available Metadata Types via the 'describeMetadata' API call
MetadataService.MetadataPort service = createService();
MetadataService.DescribeMetadataResult describeResult = service.describeMetadata(METADATA_API_VERSION);
List<String> metadataTypeNames = new List<String>();
for(MetadataService.DescribeMetadataObject metadataObject : describeResult.metadataObjects)
{
metadataTypeNames.add(metadataObject.xmlName);
// Include child Metadata Types (such as CustomField, ValidationRule etc..)
if(metadataObject.childXmlNames!=null)
for(String childXmlName : metadataObject.childXmlNames)
if(childXmlName!=null)
metadataTypeNames.add(childXmlName);
}
// Sort Metadata Types
metadataTypeNames.sort();
MetaDataTypes = new List<SelectOption>();
for(String metadataTypeName : metadataTypeNames)
MetaDataTypes.add(new SelectOption(metadataTypeName, metadataTypeName));
// Default to first Metadata Type returned
MetaDataType = MetaDataTypes[0].getValue();
// Retrieve Metadata items for the selected Metadata Type
listMetadataItems();
return null;
}
public PageReference listMetadataItems()
{
// List Metadata items for the selected Metadata Type
MetaDataItems = new List<SelectOption>();
MetadataService.MetadataPort service = createService();
List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();
MetadataService.ListMetadataQuery queryLayout = new MetadataService.ListMetadataQuery();
if(MetaDataFolder!=null && MetaDataFolder.length()>0)
queryLayout.folder = MetaDataFolder;
queryLayout.type_x = MetaDataType;
queries.add(queryLayout);
MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, METADATA_API_VERSION);
// Sort
List<String> fullNames = new List<String>();
if(fileProperties!=null)
{
for(MetadataService.FileProperties fileProperty : fileProperties)
fullNames.add(fileProperty.fullName);
fullNames.sort();
for(String fullName : fullNames)
MetaDataItems.add(new SelectOption(fullName,EncodingUtil.urlDecode(fullName, 'UTF-8')));
}
return null;
}
public PageReference retrieveMetadataItem()
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Retrieving metadata...'));
// Reset state from any previous requests
MetaDataRetrieveZip = null;
MetadataFiles = null;
// Construct unmanaged package with list of desired components to retrieve in zip
MetadataService.MetadataPort service = createService();
MetadataService.RetrieveRequest retrieveRequest = new MetadataService.RetrieveRequest();
retrieveRequest.apiVersion = METADATA_API_VERSION;
retrieveRequest.packageNames = null;
retrieveRequest.singlePackage = true;
retrieveRequest.specificFiles = null;
retrieveRequest.unpackaged = new MetadataService.Package_x();
retrieveRequest.unpackaged.types = new List<MetadataService.PackageTypeMembers>();
MetadataService.PackageTypeMembers packageType = new MetadataService.PackageTypeMembers();
packageType.name = MetaDataType;
packageType.members = new String[] { MetadataFolder, MetaDataItem };
retrieveRequest.unpackaged.types.add(packageType);
AsyncResult = service.retrieve(retrieveRequest);
return null;
}
public PageReference checkAsyncRequest()
{
// Check the status of the retrieve request
MetadataService.MetadataPort service = createService();
MetadataService.RetrieveResult retrieveResult = service.checkRetrieveStatus(AsyncResult.Id, true);
if(retrieveResult.done)
{
// Errors?
if(retrieveResult.status != 'Succeeded')
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, retrieveResult.errorMessage));
AsyncResult = null;
}
else
{
// Place Base64 encoded zip data onto the page for the JSZip library to handle
MetaDataRetrieveZip = retrieveResult.zipFile;
MetadataFiles = new List<MetadataFile>();
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Expanding...'));
AsyncResult = null;
}
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Retrieving metadata...'));
}
return null;
}
public PageReference receiveMetadataZipFile()
{
// In this example the retrieved metadata is stored in viewstate in production cases you would probably use a custom object / attachment
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Expanding ' + MetaDataFileName + '...'));
MetadataFile metaDataFile = new MetadataFile();
metaDataFile.fullName = MetaDataFileName;
metaDataFile.content = MetaDataFileData;
MetadataFiles.add(metaDataFile);
return null;
}
public PageReference receiveComplete()
{
// Completed, cleared Base64 encoded zip data from viewstate
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Metadata retrieve completed'));
MetaDataRetrieveZip = null;
return null;
}
private static MetadataService.MetadataPort createService()
{
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
return service;
}
/**
* Simple container class for retrieve metadata file, may as well leverage the Metadata API class for this
**/
public class MetadataFile extends MetadataService.MetadataWithContent
{
public String getFullname()
{
return fullName;
}
public String getContent()
{
return content;
}
}
}