private List <XElement> ChangesToXml(List <Change> changes)
        {
            var result = new List <XElement>();

            foreach (var change in changes)
            {
                var(newElement, newType) = NewValue(change);
                var(oldElement, oldType) = OldValue(change);
                // Can be CPV_MAIN, CPV_ADDITIONAL, NUTS, DATE_TIME or TEXT
                result.Add(
                    TedHelpers.Element("CORR",
                                       newType != ChangeType.Undefined && oldType == ChangeType.Undefined ?
                                       TedHelpers.ElementWithAttribute("ADD", "OBJECT", newType.ToString().ToUpper(),
                                                                       TedHelpers.Element("WHERE", $"{change.Section}: {change.Label}"),
                                                                       TedHelpers.Element("NEW_VALUE", newElement)) : null,
                                       newType == ChangeType.Undefined && oldType != ChangeType.Undefined ?
                                       TedHelpers.ElementWithAttribute("DELETE", "OBJECT", oldType.ToString().ToUpper(),
                                                                       TedHelpers.Element("WHERE", $"{change.Section}: {change.Label}"),
                                                                       TedHelpers.Element("OLD_VALUE", oldElement)) : null,
                                       newType != ChangeType.Undefined && oldType != ChangeType.Undefined ?
                                       TedHelpers.ElementWithAttribute("REPLACE", "OBJECT", newType.ToString().ToUpper(),
                                                                       TedHelpers.Element("WHERE", $"{change.Section}: {change.Label}"),
                                                                       TedHelpers.Element("OLD_VALUE", oldElement),
                                                                       TedHelpers.Element("NEW_VALUE", newElement)) : null));
            }

            return(result);
        }
        /// <summary>
        /// Section IV: Procedure Information
        /// </summary>
        /// <returns>The PROCEDURES_ICAR XElement</returns>
        private XElement ProcedureInformation()
        {
            var previousOjs = _notice.TenderingInformation.Defence?.PreviousPriorInformationNoticeOjsNumber ?? new OjsNumber();

            if (_notice.Type == NoticeType.DefenceContractAward)
            {
                previousOjs = _notice.TenderingInformation.Defence.PreviousContractNoticeOjsNumber;
            }
            if (_notice.Type == NoticeType.ExAnte)
            {
                previousOjs = _notice.TenderingInformation.Defence.PreviousExAnteOjsNumber;
            }
            return(TedHelpers.Element("PROCEDURES_ICAR",
                                      TedHelpers.Element("TYPE_OF_PROCEDURE_CORRIGENDUM",
                                                         TedHelpers.ElementWithAttribute("TYPE_OF_PROCEDURE_DETAIL_FOR_ICAR", "VALUE", ProcedureType())),
                                      TedHelpers.Element("ADMINISTRATIVE_INFORMATION",
                                                         TedHelpers.PElement("FILE_REFERENCE_NUMBER", _notice.Project?.ReferenceNumber),
                                                         TedHelpers.Element("SIMAP_ESENDER_NOTICE_REFERENCE",
                                                                            TedHelpers.ElementWithAttribute("SIMAP_ESENDER", "VALUE", "OJS_ESENDER"),
                                                                            TedHelpers.ElementWithAttribute("LOGIN", "CLASS", "B",
                                                                                                            TedHelpers.Element("ESENDER_LOGIN", _eSenderLogin)),
                                                                            TedHelpers.Element("NO_DOC_EXT", _notice.NoticeNumber)),
                                                         TedHelpers.Element("NOTICE_PUBLICATION",
                                                                            TedHelpers.Element("NOTICE_NUMBER_OJ", previousOjs.Number),
                                                                            TedHelpers.DateElement("DATE_OJ", previousOjs.Date)),
                                                         TedHelpers.DateElement("ORIGINAL_DISPATCH_DATE", _parent.TedPublishRequestSentDate != DateTime.MinValue ? _parent.TedPublishRequestSentDate :  null))
                                      //TODO (TuomasT): Allow setting original date manually
                                      ));
        }
 /// <summary>
 /// Section I: Contracting authority
 /// </summary>
 /// <param name="organisation">The organisation</param>
 /// <param name="contactPerson">The contact person</param>
 /// <param name="communicationInformation">I.3 Communication</param>
 /// <returns>CONTRACTING_BODY XElement</returns>
 private XElement ContractingBody(OrganisationContract organisation, ContactPerson contactPerson, CommunicationInformation communicationInformation)
 {
     return(TedHelpers.Element("AUTH_ENTITY_ICAR",
                               TedHelpers.Element("NAME_ADDRESSES_CONTACT_ICAR",
                                                  TedHelpers.INC_01("CA_CE_CONCESSIONAIRE_PROFILE", organisation, contactPerson),
                                                  TedHelpers.Element("INTERNET_ADDRESSES_ICAR",
                                                                     TedHelpers.Element("URL_GENERAL", _notice.Project.Organisation.Information.MainUrl),
                                                                     TedHelpers.Element("URL_INFORMATION", communicationInformation.ElectronicAccess))),
                               // DIRECTIVE_2004_17 = CA
                               // DIRECTIVE_2004_18 = CE
                               TedHelpers.ElementWithAttribute("TYPE_OF_PURCHASING_BODY", "VALUE", "DIRECTIVE_2004_17")));
 }
        private static Tuple <List <XElement>, ChangeType> NewValue(Change change)
        {
            List <XElement> newValue;
            ChangeType      type;

            if (change.NewText != null && change.NewText.Length > 0 && !string.IsNullOrEmpty(change.NewText[0]))
            {
                type     = ChangeType.Text;
                newValue = new List <XElement> {
                    TedHelpers.PElement("TEXT", change.NewText)
                };
            }
            else if (change.NewNutsCodes != null && change.NewNutsCodes.Length > 0 && !string.IsNullOrEmpty(change.NewNutsCodes[0]))
            {
                type     = ChangeType.Nuts;
                newValue = new List <XElement> {
                    TedHelpers.PElement("NUTS", change.NewText)
                };
            }
            else if (change.NewDate != null && change.NewDate != DateTime.MinValue)
            {
                type     = ChangeType.Date;
                newValue = new List <XElement> {
                    TedHelpers.DateTimeElement("DATE_TIME", change.NewDate)
                };
            }
            else if (change.NewMainCpvCode != null && !string.IsNullOrEmpty(change.NewMainCpvCode.Code))
            {
                type     = ChangeType.Cpv;
                newValue = new List <XElement> {
                    TedHelpers.Element("CPV_MAIN",
                                       TedHelpers.ElementWithAttribute("CPV_CODE", "CODE", change.NewMainCpvCode.Code),
                                       change.NewMainCpvCode.VocCodes?.Select(x => TedHelpers.ElementWithAttribute("CPV_SUPPLEMENTARY_CODE", "CODE", x.Code)))
                };
            }
            else if (change.NewAdditionalCpvCodes != null && change.NewAdditionalCpvCodes.Count > 0)
            {
                type     = ChangeType.Cpv;
                newValue = change.NewAdditionalCpvCodes.Select(x =>
                                                               TedHelpers.Element("CPV_ADDITIONAL",
                                                                                  TedHelpers.ElementWithAttribute("CPV_CODE", "CODE", x.Code),
                                                                                  x.VocCodes?.Select(y => TedHelpers.ElementWithAttribute("CPV_SUPPLEMENTARY_CODE", "CODE", y.Code)))
                                                               ).ToList();
            }
            else
            {
                type     = ChangeType.Undefined;
                newValue = null;
            }

            return(new Tuple <List <XElement>, ChangeType>(newValue, type));
        }
 /// <summary>
 /// #  XSD name : F02_2014
 /// #  RELEASE : "R2.0.9.S03"
 /// #  Intermediate release number 007-20181030
 /// #  Last update : 30/10/2018
 /// #  Form : Contract notice
 /// </summary>
 private XElement NoticeBody()
 {
     return(TedHelpers.Element("FORM_SECTION",
                               TedHelpers.Element("PRIOR_INFORMATION_DEFENCE", new XAttribute("LG", _notice.Language), new XAttribute("CATEGORY", "ORIGINAL"), new XAttribute("FORM", "16"), new XAttribute("VERSION", "R2.0.8.S04"),
                                                  TedHelpers.ElementWithAttribute("FD_PRIOR_INFORMATION_DEFENCE", "CTYPE", _notice.Project.ContractType.ToTEDFormat(),
                                                                                  _helper.ContractingBody(_notice.Project, _notice.ContactPerson, _notice.CommunicationInformation, _notice.Type),
                                                                                  _helper.ObjectContract(),
                                                                                  _helper.ConditionsInformation(_notice.ConditionsInformationDefence),
                                                                                  _helper.Procedure(_notice.ProcedureInformation),
                                                                                  _helper.ComplementaryInformation()
                                                                                  )
                                                  )
                               ));
 }
 private XElement CAFields(string elementName, OrganisationContract organisation, ContactPerson contactPerson)
 {
     return(TedHelpers.Element(elementName,
                               TedHelpers.Element("OFFICIALNAME", organisation.Information.OfficialName),
                               TedHelpers.Element("NATIONALID", organisation.Information.NationalRegistrationNumber),
                               TedHelpers.Element("ADDRESS", organisation.Information.PostalAddress.StreetAddress),
                               TedHelpers.Element("TOWN", organisation.Information.PostalAddress.Town),
                               TedHelpers.Element("POSTAL_CODE", organisation.Information.PostalAddress.PostalCode),
                               TedHelpers.ElementWithAttribute("COUNTRY", "VALUE", organisation.Information.PostalAddress.Country),
                               TedHelpers.Element("CONTACT_POINT", contactPerson.Name), TedHelpers.Element("PHONE", contactPerson.Phone),
                               TedHelpers.Element("E_MAIL", contactPerson.Email),
                               organisation.Information.NutsCodes.ToList().Select(x => new XElement(TedHelpers.n2016 + "NUTS", new XAttribute("CODE", x))),
                               TedHelpers.Element("URL_GENERAL", organisation.Information.MainUrl)
                               ));
 }