TestandtargetServiceImpl.java 22.6 KB
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.analytics.testandtarget.Campaign;
import com.day.cq.analytics.testandtarget.Folder;
import com.day.cq.analytics.testandtarget.HTMLOffer;
import com.day.cq.analytics.testandtarget.ListOffersRequest;
import com.day.cq.analytics.testandtarget.ListSegmentsRequest;
import com.day.cq.analytics.testandtarget.Offer;
import com.day.cq.analytics.testandtarget.PerformanceReport;
import com.day.cq.analytics.testandtarget.PerformanceReportItem;
import com.day.cq.analytics.testandtarget.PerformanceReportRequest;
import com.day.cq.analytics.testandtarget.Recipe;
import com.day.cq.analytics.testandtarget.Report;
import com.day.cq.analytics.testandtarget.ReportType;
import com.day.cq.analytics.testandtarget.Reports;
import com.day.cq.analytics.testandtarget.Resolution;
import com.day.cq.analytics.testandtarget.Sample;
import com.day.cq.analytics.testandtarget.SaveOfferRequest;
import com.day.cq.analytics.testandtarget.Segment;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.TestandtargetHttpClient;
import com.day.cq.analytics.testandtarget.TestandtargetUnsupportedApiOperationException;
import com.day.cq.analytics.testandtarget.ViewOfferRequest;
import com.day.cq.analytics.testandtarget.ViewOfferResponse;
import com.day.cq.analytics.testandtarget.impl.RestApiAdapter;
import com.day.cq.analytics.testandtarget.impl.TestandtargetCampaign;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.analytics.testandtarget.impl.XmlApiAdapter;
import com.day.cq.analytics.testandtarget.impl.model.TargetAccountOptions;
import com.day.cq.analytics.testandtarget.impl.model.TntProfileAttribute;
import com.day.cq.analytics.testandtarget.util.CampaignType;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
public class TestandtargetServiceImpl
implements TestandtargetPrivateService {
    private static final String PN_API_KIND = "apiKind";
    private final Logger log;
    @Reference
    private CryptoSupport cryptoSupport;
    @Reference
    private TestandtargetHttpClient client;

    public TestandtargetServiceImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public Folder listFolders(Configuration configuration) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.XML) {
            return new XmlApiAdapter(this.client, this.cryptoSupport).listFolders(configuration);
        }
        throw new TestandtargetUnsupportedApiOperationException("folderList", apiKind.name());
    }

    @Override
    public long createHTMLOffer(Configuration configuration, SaveOfferRequest request) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        switch (apiKind) {
            case XML: {
                return new XmlApiAdapter(this.client, this.cryptoSupport).createHTMLOffer(configuration, request);
            }
            case REST: {
                return new RestApiAdapter(this.client, this.cryptoSupport).createOffer(configuration, request);
            }
        }
        throw new TestandtargetUnsupportedApiOperationException("createHTMLOffer", apiKind.name());
    }

    @Override
    public void createHTMLOffer(Configuration configuration, String offerName, String folderId, String content) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        switch (apiKind) {
            case XML: {
                new XmlApiAdapter(this.client, this.cryptoSupport).createHTMLOffer(configuration, offerName, folderId, content);
                break;
            }
            case REST: {
                new RestApiAdapter(this.client, this.cryptoSupport).createOffer(configuration, new SaveOfferRequest(offerName, content));
                break;
            }
            default: {
                throw new TestandtargetUnsupportedApiOperationException("createHTMLOffer", apiKind.name());
            }
        }
    }

    @Override
    public HTMLOffer getHTMLOffer(Configuration configuration, String offerName) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.XML) {
            return new XmlApiAdapter(this.client, this.cryptoSupport).getHTMLOffer(configuration, offerName);
        }
        throw new TestandtargetUnsupportedApiOperationException("getHtmlOfferContent", apiKind.name());
    }

    @Override
    public ViewOfferResponse getReusableOffer(Configuration configuration, ViewOfferRequest request) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).getOffer(configuration, request);
        }
        throw new TestandtargetUnsupportedApiOperationException("getReusableOffer", apiKind.name());
    }

    @Override
    public Collection<Offer> listOffers(Configuration configuration, String folderId) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.XML) {
            return new XmlApiAdapter(this.client, this.cryptoSupport).listOffers(configuration, folderId);
        }
        throw new TestandtargetUnsupportedApiOperationException("listOffers(folderId)", apiKind.name());
    }

    @Override
    public Collection<ViewOfferResponse> listOffers(Configuration configuration, ListOffersRequest request) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).listOffers(configuration, request);
        }
        throw new TestandtargetUnsupportedApiOperationException("listOffers", apiKind.name());
    }

    @Override
    public String publishOffer(Configuration configuration, String name, String url, String id) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.XML) {
            return new XmlApiAdapter(this.client, this.cryptoSupport).publishOffer(configuration, name, url, id);
        }
        throw new TestandtargetUnsupportedApiOperationException("saveWidgetOffer", apiKind.name());
    }

    @Override
    public String unpublishOffer(Configuration configuration, String name, String url, String id) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.XML) {
            return new XmlApiAdapter(this.client, this.cryptoSupport).unpublishOffer(configuration, name, url, id);
        }
        throw new TestandtargetUnsupportedApiOperationException("deleteWidgetOffer", apiKind.name());
    }

    @Override
    public String createWidgetOffer(Configuration configuration, String name, String url, String id) throws TestandtargetException {
        return this.publishOffer(configuration, name, url, id);
    }

    @Override
    public Map<Integer, String> listCampaigns(Configuration configuration, Date before, Date after, String environment, String name, String state, String label) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        switch (apiKind) {
            case XML: {
                return new XmlApiAdapter(this.client, this.cryptoSupport).listCampaigns(configuration, before, after, environment, name, state, label);
            }
            case REST: {
                if (!StringUtils.isEmpty((String)environment)) {
                    this.log.warn("Searching campaigns by environment is not supported by the {} API, ignoring", (Object)apiKind);
                }
                if (!StringUtils.isEmpty((String)label)) {
                    this.log.warn("Searching campaigns by label is not supported by the {} API, ignoring", (Object)apiKind);
                }
                return new RestApiAdapter(this.client, this.cryptoSupport).listCampaigns(configuration, null, null, name, state);
            }
        }
        throw new TestandtargetUnsupportedApiOperationException("campaignList", apiKind.name());
    }

    @Override
    public boolean setCampaignState(Configuration configuration, String campaignState, String testAndTargetCampaignId, String thirdPartyCampaignId) throws TestandtargetException {
        return this.setCampaignState(configuration, campaignState, testAndTargetCampaignId, thirdPartyCampaignId, CampaignType.LANDING_PAGE);
    }

    @Override
    public boolean setCampaignState(Configuration configuration, String campaignState, String testAndTargetCampaignId, String thirdPartyCampaignId, CampaignType campaignType) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        switch (apiKind) {
            case XML: {
                return new XmlApiAdapter(this.client, this.cryptoSupport).setCampaignState(configuration, campaignState, testAndTargetCampaignId, thirdPartyCampaignId);
            }
            case REST: {
                return new RestApiAdapter(this.client, this.cryptoSupport).setCampaignState(configuration, campaignState, testAndTargetCampaignId, thirdPartyCampaignId, campaignType);
            }
        }
        throw new TestandtargetUnsupportedApiOperationException("setCampaignState", apiKind.name());
    }

    @Override
    public void saveCampaign(Configuration configuration, String thirdPartyId, TestandtargetCampaign campaign) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        switch (apiKind) {
            case XML: {
                new XmlApiAdapter(this.client, this.cryptoSupport).saveCampaign(configuration, thirdPartyId, campaign);
                break;
            }
            case REST: {
                new RestApiAdapter(this.client, this.cryptoSupport).saveCampaign(configuration, campaign);
                break;
            }
            default: {
                throw new TestandtargetUnsupportedApiOperationException("saveCampaign", apiKind.name());
            }
        }
    }

    private ApiKind getApiKind(Configuration configuration) {
        String apiKind = (String)configuration.getInherited("apiKind", (Object)ApiKind.XML.toString());
        if (ApiKind.XML.toString().equals(apiKind)) {
            return ApiKind.XML;
        }
        if (ApiKind.REST.toString().equals(apiKind)) {
            return ApiKind.REST;
        }
        throw new UnsupportedOperationException("Can't handle apiKind " + apiKind + " , valid values are " + Arrays.toString((Object[])ApiKind.values()));
    }

    @Override
    public void checkCredentials(String clientcode, String email, String password) throws TestandtargetException {
        new XmlApiAdapter(this.client, this.cryptoSupport).checkCredentials(clientcode, email, password);
    }

    @Override
    public Reports getPerformanceReport(Configuration configuration, ReportType reportType, String campaignId, String thirdPartyCampaignId, Date start, Date end, Resolution resolution) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.XML) {
            return new XmlApiAdapter(this.client, this.cryptoSupport).getPerformanceReport(configuration, reportType, campaignId, thirdPartyCampaignId, start, end, resolution);
        }
        throw new TestandtargetUnsupportedApiOperationException("report", apiKind.name());
    }

    @Override
    public PerformanceReport getPerformanceReport(Configuration configuration, PerformanceReportRequest request) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        switch (apiKind) {
            case XML: {
                Reports deprecatedReport = this.getPerformanceReport(configuration, ReportType.IMPRESSION, String.valueOf(request.getCampaignId()), null, request.getStart(), request.getEnd(), Resolution.DAY);
                return this.convertFromDeprecatedReport(deprecatedReport);
            }
            case REST: {
                return new RestApiAdapter(this.client, this.cryptoSupport).getCampaignPerformanceReportSummary(configuration, request.getCampaignId(), request.getParameters());
            }
        }
        throw new TestandtargetUnsupportedApiOperationException("report", apiKind.name());
    }

    private PerformanceReport convertFromDeprecatedReport(Reports deprecatedReport) {
        PerformanceReport newReport = new PerformanceReport();
        LinkedHashMap<String, RecipeDataAccumulator> recipeData = new LinkedHashMap<String, RecipeDataAccumulator>();
        for (Report report : deprecatedReport.getReports()) {
            for (Sample sample : report.getSamples()) {
                for (Recipe recipe : sample.getRecipes()) {
                    if (!"TESTING".equals(recipe.getTrafficType())) continue;
                    String recipeId = recipe.getId();
                    String recipeName = recipe.getName();
                    int impressions = recipe.getStepValueByName("Display mboxes");
                    int conversions = recipe.getConversionCount();
                    RecipeDataAccumulator recipeAccu = (RecipeDataAccumulator)recipeData.get(recipeId);
                    if (recipeAccu == null) {
                        recipeAccu = new RecipeDataAccumulator(recipeName, impressions, conversions);
                        recipeData.put(recipeId, recipeAccu);
                        continue;
                    }
                    recipeAccu.add(impressions, conversions);
                }
            }
        }
        for (RecipeDataAccumulator recipeAccu : recipeData.values()) {
            PerformanceReportItem item = new PerformanceReportItem();
            item.setConversionCount(recipeAccu.getConversions());
            item.setEntryCount(recipeAccu.getImpressions());
            item.setExperienceName(recipeAccu.getName());
            newReport.addItem(item);
        }
        return newReport;
    }

    @Override
    public List<Segment> listSegments(Configuration configuration, ListSegmentsRequest request) throws TestandtargetException {
        return new RestApiAdapter(this.client, this.cryptoSupport).listSegments(configuration, request);
    }

    @Override
    public List<String> getMboxActivityCollisions(Configuration configuration, String mboxName, String mboxUrl) throws TestandtargetException {
        return new RestApiAdapter(this.client, this.cryptoSupport).getMboxActivityCollisions(configuration, mboxName, mboxUrl);
    }

    public void migrateCampaign(String campaignPath) throws TestandtargetException {
    }

    @Override
    public List<String> listMboxNames(Configuration configuration) throws TestandtargetException {
        return new RestApiAdapter(this.client, this.cryptoSupport).listMboxes(configuration);
    }

    @Override
    public Campaign getCampaignByThirdPartyId(Configuration configuration, String thirdPartyId, CampaignType campaignType) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).getCampaignByThirdPartyId(configuration, thirdPartyId, campaignType);
        }
        if (apiKind == ApiKind.XML) {
            return new XmlApiAdapter(this.client, this.cryptoSupport).getCampaignByThirdPartyId(configuration, thirdPartyId);
        }
        throw new TestandtargetUnsupportedApiOperationException("getCampaignByThirdPartyId", apiKind.name());
    }

    @Override
    public Campaign getCampaignById(Configuration configuration, long campaignId, CampaignType campaignType) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).getCampaignById(configuration, campaignId, campaignType);
        }
        throw new TestandtargetUnsupportedApiOperationException("getCampaignByThirdPartyId", apiKind.name());
    }

    @Override
    public String createAudience(Configuration configuration, String audienceJson, boolean isReusable, String modifiedByUser) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).createSegment(configuration, "", audienceJson, isReusable, modifiedByUser);
        }
        throw new TestandtargetUnsupportedApiOperationException("createAudience", apiKind.name());
    }

    @Override
    public String updateAudience(Configuration configuration, String audienceJson, long audienceId, boolean isReusable, String modifiedByUser) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).updateSegment(configuration, audienceId, audienceJson, isReusable, modifiedByUser);
        }
        throw new TestandtargetUnsupportedApiOperationException("updateAudience", apiKind.name());
    }

    @Override
    public void updateAccountOptions(Configuration configuration) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.XML) {
            throw new TestandtargetUnsupportedApiOperationException("updateAccountOptions", apiKind.name());
        }
        TargetAccountOptions accountOptions = new RestApiAdapter(this.client, this.cryptoSupport).getAccountOptions(configuration);
        if (accountOptions.getProductOptions() != null) {
            try {
                String optionsString = StringUtils.join(accountOptions.getProductOptions(), (String)",");
                String encryptedOptions = new String(this.cryptoSupport.protect(optionsString));
                ResourceResolver resolver = configuration.getResource().getResourceResolver();
                if (configuration.getContentResource() != null) {
                    ModifiableValueMap mvm = (ModifiableValueMap)configuration.getContentResource().adaptTo(ModifiableValueMap.class);
                    mvm.put((Object)"accountOptions", (Object)encryptedOptions);
                    resolver.commit();
                }
            }
            catch (CryptoException e) {
                throw new TestandtargetException((Throwable)e);
            }
            catch (PersistenceException e) {
                throw new TestandtargetException((Throwable)e);
            }
        }
    }

    @Override
    public List<TntProfileAttribute> getProfileAttributes(Configuration configuration) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).getProfileAttributes(configuration);
        }
        throw new TestandtargetUnsupportedApiOperationException("profileAttributes", apiKind.name());
    }

    @Override
    public List<TntProfileAttribute> getCustomProfileAttributes(Configuration configuration) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).getCustomProfileAttributes(configuration);
        }
        throw new TestandtargetUnsupportedApiOperationException("profileAttributes/mbox", apiKind.name());
    }

    @Override
    public String searchMobileAttributes(Configuration configuration, String searchType, String paramName, String searchTerm) throws TestandtargetException {
        ApiKind apiKind = this.getApiKind(configuration);
        if (apiKind == ApiKind.REST) {
            return new RestApiAdapter(this.client, this.cryptoSupport).searchMobileAttributes(configuration, searchType, paramName, searchTerm);
        }
        throw new TestandtargetUnsupportedApiOperationException("search/mobile", apiKind.name());
    }

    protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
    }

    protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
        if (this.cryptoSupport == cryptoSupport) {
            this.cryptoSupport = null;
        }
    }

    protected void bindClient(TestandtargetHttpClient testandtargetHttpClient) {
        this.client = testandtargetHttpClient;
    }

    protected void unbindClient(TestandtargetHttpClient testandtargetHttpClient) {
        if (this.client == testandtargetHttpClient) {
            this.client = null;
        }
    }

    private static class RecipeDataAccumulator {
        private String name;
        private int impressions;
        private int conversions;

        public RecipeDataAccumulator(String name, int impressions, int conversions) {
            this.name = name;
            this.impressions = impressions;
            this.conversions = conversions;
        }

        public String getName() {
            return this.name;
        }

        public int getImpressions() {
            return this.impressions;
        }

        public int getConversions() {
            return this.conversions;
        }

        public void add(int impressions, int conversions) {
            this.impressions += impressions;
            this.conversions += conversions;
        }
    }

    static enum ApiKind {
        XML,
        REST;
        

        private ApiKind() {
        }
    }

}